Instantiating an Object
An Object is an instance of a Class. After creating an Object, you can access the member variables and methods of the object and assign values to them. An Object is declared in the same way that a variable of a primitive type is declared.
String sampleStr; //This code is used to define an object name of String type.
You can define a variable that will be used to refer to an object or an instance of a class.
When you create an object, you need to assign memory to the object. This is done using the new operator. The new operator is followed by the class name and parentheses. The syntax for creating an object is displayed below:
class_name variable_name = new class_name();
Example: Test aTest = new Test();
This code is used to create an instance of the Test class. The variable aTest is decleared. Notice that this is an arbitary variable name. This variable is used to refer to the instance of the Test class. You can also state that the name of this particular Test instance is aTest.
When multiple instances are created, each instance maintains a seperate copy of the member variables of the class.
Member Access – Dot Notation
The member variables declared in a class are accessed to assign values or manipulate the values stored in them. The public member variables may be accessed from any object using dot notation. In addition, member variables that are declared without a modifier are considered to have package scope. These variables may be accessed using dot notation from any object defined in the same package. Whereas private members cannot be accessed using dot notations. We need to define public methods [Getters] to access the private members and Setters for assigning values to private member variables
HINT:: Getters and Setters
A getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property.
A scope defines the logical boundary for accessing a variable.
HINT: Java allows you to use the same identifier for multiple variables as long as they are declared in a different scope.
A variable is in the member scope if it is accessible to the methods of the class. This variable is declared as the member variable of the class (i.e) Variables declared directly after class declarations.
A variable is considered to be in the local scope if it is declared within any block of code or if its declared inside a loop condition. A block is a group of statements that are enclosed within braces.
HINT: How Compiler handles?
The Java compiler refers to the variable definitions in a sequence to distinguish the current value of the variable. If first checks the variable in the current block. The current scope depends on the current statement being executed. If the compiler is unable to find the variable in the current block, it searches the variable in the current class. Finally, the compiler searches in the superclasses of the current class.
Variable Access – ‘this’ Keyword
The ‘this’ keyword can be used to access the member variables of the current object. These member variables of an object are also known as instance variables.
What is Instance Variables?
When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables.
What is Class Variables?
Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
Benefits of Static Variables