In java we can define object as the instance of class. Object is used to instantiate all the non-static members. In java everything is based on Object, it means memory are allocating for the objects only. Everything we are dealing with is the help of object only , it may be calling method, accessing instance variable and many more. 

Important point regarding Object in java

1> Object creation is with the use of new operator.

2> All the objects are created in the heap memory which is a part of main memory.

3> Whenever we are creating JVM will first call to default constructor if no constructor in defined in the class to initialize the instance variable. 

4>We can define object for any class, it may be predefined class or use-defined class. 
For ex-There is predefined classes available  for every primitive data type in Object class so that we can use create object for primitive type as well called as wrapper class.

Syntax for defining Object :
< class_name >   <reference_variable >  =  new  < class_name >( [value] ) ;
for ex- Hello h = new Hello( ) ;

In the above syntax we can see that class_name is user defined class and with the new operator we had defined a constructor of the class where we can pass value as well but it is optional. 

5> Reference value is always pointing to the object of the class with contain the reference value where the object is stored in the memory.

6> If not object is created then reference variable is containing NULL as the reference value indicating not pointing any object.

Let me make you more understood with the help of an example :

public class prog{

int x ;                    // instance variable

String name ;             // instance variable

long ln ;                // instance variable

float f ;               // instance variable

void display(){         // Instance method

System.out.println("Display method");   

}


public static void main(String sush[ ]) {

prog p = new prog( );           // Object created

p.display();                  // Instance method called

    }
}

Here variable " x ", " ln "," f " and " name " is instance variable and display ( ) is an instance method . It is only called with the help of object only. 

Here we create and object in the main ( ) by which JVM will internally call a default constructor which is responsible for initializing instance variable with the default value if not initialized by the programmer manually. 

Diagrammatic representation of Object 

Representation of an Object
Here we can see that as per above example " p " is a reference variable indicating the object which contain the instance variable stored in the heap memory.




You all must be wondering that what happened to instance method  ?
Instance method will be called by the object and get executed in the stack memory always. And it is only executed when we are calling method manually in our program with the help an object otherwise it won't execute self. 

Note : If we are not using any new operator then it will remain as a reference value which store null, means not pointing any object.

0 comments:

Post a Comment

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic