Classes & Objects

class keyword

            class is standard inbuilt keyword of java language, it creates the block that encapsulates the member variables and methods.

Syntax:

class <Class Name>

{

_______________

_______________                   Member Variables & Methods

_______________

}

There are three types of objects available in java language as given below.

  1. Declared object or null object.
  2. Memory allocated object. (Creation of Object)
  3. Reference object.

Declared object

Whenever any object is declared by its class name (type) then that object is not linked with any memory location such object cannot access any member of class and therefore such object is known as declared object or null object.

Example:

class Data

{

private int a;

public void set(int n)

{

a=n;

}

public void show()

{

System.out.println(“Value=”+a);

}

}

class UseMe

{

public static void main(String args[])

{

Data d1;

}

}

In the above example, object d1 is declared as Data in main program. During the execution of program, execution control will not allocate memory for the object d1 and this object cannot access any member of class such object (d1) is known as declared object or null object.

Creation of Object

Whenever any object is created by new operator then that object will allocate memory and the allocated memory will contain member variables according to the number of member variables available in class, such allocated memory is known as instance and the object can access all the public members of class. This concept is known as creation of object.

Example:

class Data

{

private int a;

public void set(int n)

{

a=n;

}

public void show()

{

System.out.println(“Value=”+a);

}

}

class UseMe

{

public static void main(String args[])

{

Data d1=new Data();

d1.set(10);

d1.show();

}

}

Output: Value=10

In the above example, new operator will allocate memory according to the number of member variables available in Data class and initialized the value of all the member variables by using implicit default constructor, after the initialization of member variables of object, assignment operator will assign the complete allocated memory to the object d1 and therefore object d1 will able to access all the public members of Data class.

Reference Object

Whenever assignment operator assigns data from one object to another object then java interpreter will not copy the data from one object to another object instead it will create the reference of source object to target object i.e. both the object will linked with same memory location and can access same values as well as can change the value of member variables.

Example:

class Data

{

private int a;

public void set(int n)

{

a=n;

}

public void show()

{

System.out.println(“Value=”+a);

}

}

class UseMe

{

public static void main(String args[])

{

Data d1=new Data();

d1.set(10);

d1.show();

Data d2=d1;

d2.set(20);

d2.show();

d1.show();

}

}

Output:

Value=10

Value=20

Value=20

 

During the execution of assignment statement of above program, execution control will not copy the data from object d1 to object d2 instead object d2 will create reference with the memory of object d1 i.e. both the objects d1 & d2 will linked with same memory location and will have the same value.


Leave a Reply

Your email address will not be published. Required fields are marked *