Objects And Variables

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.

Variable Scope

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.

Member 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.

Local Scope
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

  • They are initialized once for all instances of the class.
  • They can be accessed directly using the class name, without a reference to an instance of the class.
  • They are used to store a single value for all instances of a class.
  • Related Posts: Introduction to Java | Classes, Methods & Objects | Datatypes | Operators | Conditional Flow Control | Iterative Flow Control | Jump Statements


    2 responses to “Objects And Variables

    1. very interesting, but I don’t agree with you

    2. fine and good
      useful information in java basics

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s