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.

Related Posts:

  • VAR-ARGS ( Variable Arguments ) in Java It is a new feature introduced from java 5. It is same as array but in case of variable arguments we are passing directly value were as in case of array we need to create manually an array object. The task we are doing of c… Read More
  • Command line argument in Java Command line argument is the facility in java to pass the string value while executing the class file after the compilation.While executing the class file we can give as many string value separated with the space. These valu… Read More
  • Control statement in Java Control statement in any programming language is used to control the flow of the statement in the application. That is why it is termed as " control ". There are three types of control statement i.e 1> Conditional control… Read More
  • Method overloading in java Method overloading in java is the facility which allow us to write same name method with different arguments and hence different implementation.So now with the help of method overloading we can use same method name with diff… Read More
  • Object in Java 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 … Read More

0 comments:

Post a Comment

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic