Instance fields

The variables which are declared with the class without static modifier called instance fields or non-static variables.

For instance fields memory is always allocated with in the object in heap memory. Instance variables can be referred using object reference. By default instance field is initialised by their corresponding data type values of default value.

Object and Class

Class: Class is a keyword, with the help of class keyword we can define our own data type called user defined data type.

Class is a collection of fields and methods, where the fields are used to hold the data and methods are used to perform the operation on the data.

In java class members can be initialised whereas in C++ doen’t support initialisation.

Object: A real time object is nothing but instance of a class.

In general class is a logical representation it doesn’t occupy any space in the memory. In order to store the values we required a physical representation(object).

To create object in the we need 2 things.

  • Reference variable
  • new operator & constructor

Example: Object object=new Object();

Here Object is class, object is reference variable and new is a operator which is used to allocate the memory in the heap dynamically. Object() is a constructor.

For every reference variable 4 byte of memory is allocated in the stock. It is existed with partitions one is type(class name) and other is hashcode(address of object).