Essential Java Programming - Fields and Variables
The course is part of this learning path
Essential Java Programming
This training course takes you through many of the essential Java programming features. We’ll review in depth features like Language Statements, Using Strings, Subclasses, Fields and Variables, Using Arrays, Java Packages and Visibility, and much more.
- Understand key Java language statements and keywords
- Be able to develop confidently with Strings
- Be able to implement specialization using subclasses
- Work with fields and variables
- Implement Arrays to store multiple values in a single variable
- Learn how to structure your Java code using Packages and Visibility
- A basic understanding of software development
- A basic understanding of the software development life cycle
- Software Engineers interested in learning Java to develop applications
- Software Architects interested in learning Java to design applications
- Anyone interested in basic Java application development and associated tooling
- Anyone interested in understanding the basics of the Java SDK
- [Lecturer] Okay, welcome back. In this lecture we'll familiarize yourself with fields and variables, and how to use them to maintain state. We'll cover items such as initialization of instance variables using default values. Being able to distinguish between instance variables and method variables within a method. Initialization of method variables prior to use. Explaining the difference between the terms, field and variable. Listing the default values for instance variables of type String, int, double, and boolean. And the use of the keyword used to create constants within Java. In Java a field refers to a member of the class. It is defined in the class block and is accessible by all methods declared within the class. A local variable is the term used to denote a variable of a method. This includes the method arguments and the primitive and object references declared within the body of the method. These variables cease to exist once the execution returns from the method. Default values. When objects are created, instance variables are automatically set to their default values. Integer types become zero. Float types, zero dot zero. Boolean is set to false. Char, hex zero. And reference types are null. Object references, as just mentioned, are set to null, which is distinct from zero. If you try to use a null object reference, the program will cause a runtime error to occur. We will learn how to process this error later in the exceptions lecture of this course. Block scoping rules. Examples of code blocks in Java are, the method, a block within the class. Language statements, for example if, for, while, etc. Exception handling code. Explicit code blocks, these are not often used. Local variables within blocks hide fields of the same name outside of the block. And a variable is invalidated when the block in which it was declared is no longer being executed. Again, as a reminder, local variables with the same name as instance variables will hide the instance variable from the method. You must use the this reference to see the instance variable if you have an instance variable with the same name as a local variable. Again, you can always see out of a block, but not into a block of code. Every object has a this pointer. It is automatically created and initialized at the time the object is created. It is often used to distinguish a field from a local variable by the same name. Final and static variables. These modifiers can only be used on fields, not local variables in a method. When a field is declared final its contents cannot be changed. It becomes a constant. Generally, final fields are named in all uppercase letters to easily distinguish them from normal fields. A static field is a field in which its contents exist within the class definition. Not within an object instance. This means that this field can be accessed by all instances of the class. And by other classes if permitted by access visibility. Often, constants are made static so that their values can be used by other classes. Static fields will be covered in more detail in the later session. In the example shown here within the class, we have declared a single static variable named circle. Typed as an int And initialized to the value 56. Within the same class, we have also declared a single instance variable, named realized, typed as an int and initialized to the value 56. Diving deeper into the static field. The static field belongs to the class and not to the instance. It is created when the class is loaded into the JVM. There is only one of these, no matter how many instances there are. All of the instances share the same single object or method.
Be aware that all instances have access to the class variable and can change its value, if it was not declared final. There needs to be an agreement in rules concerning access and its mutability. This applies to methods as well. Static methods cannot call instance methods. This is because there is no guarantee that an instance exists. Nor, if there are multiple instances, which one to call. Static fields are shared by all instances of a class. They are useful for keeping counts of number of objects created, or as a reference to a shared resource by all members of the same class. Static methods can be used without creating an instance of a class. The most familiar of these is the main method, which is used to bootstrap execution of the program. It is important to remember that a static method cannot use any of the instance variables defined in the class, without having an object reference to use. Okay consider the following questions to test... Okay, the answers to the above question are, one, instance variables have default values and do not need to be initialized before they are used. Two, local variables must always be initialized before they can be referenced. Three, local variables hide instance variables of the same name, unless the this operator is used to explicitly reference the instance variables. Four, constants are created using the final keyword. Five, constants can be referenced without instantiating the class by using the static keyword. Six, integer types default to zero. Float types default to zero dot zero. Boolean default to false. Char defaults to a hex zero and reference types default to null. Okay there completes this lecture. Go ahead and close it and we'll see you shortly in the next one.
About the Author
Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.
He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.
Jeremy holds professional certifications for both the AWS and GCP cloud platforms.