Monday, March 16, 2009

Object Memory Model (Java Part 1)

Object Memory Modal:

When you create a class, you are creating a new data type. By creating a class, you are creating a new data type that defines both the nature of the data being manipulated and the routines used to manipulate it. You can use this type to declare objects of that type. However, obtaining objects of a class is a two-step process. First you must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual physical copy of the object and assign it to the variable, by using the new operator. The new operator dynamically allocates memory for an object and returns a reference to it. This reference is the address in memory of the object allocated by new. This reference is then stored in the variable.

E.g.

MyClass obj;

Obj = new MyClass

Or it can be written as

E.g. MyClass obj = new MyClass();

In the first step obj is declared as variable of type MyClass. Now this obj variable contains a Null,

Which indicates that it does not yet point to an actual object. In the next step an actual object is created in the memory and its reference is passed to the obj variable. Actually the obj variable only contains the reference of the object which resides in the memory.

The statement “ new MyClass() “ actually runs the constructor of class MyClass. The new operator allocates memory for the object at runtime and the MyClass() executes the constructor.

What are constructors?? Hossla, Hossla, we are coming to it.

// Displays the function of refernce variable and the object.

image

After the constructor is executed a real instance of the object is created and its reference is passed to the variable. Now you can use the object methods and can perform different operations on the object, which the class offers.

MyClass Obj1 = new MyClass();

MyClass Obj2;

Obj2=Obj1;image

Two different objects can refer to the same object as shown above.

Constructors:

They are simple functions, which are executed whenever a new object is created. A constructor initializes an object immediately upon its creation. It has the same name as the class. Constructor is automatically called before the new operator completes. They don’t have any return type nor void. It is the constructor’s job to initialize the internal state of an object. The can be overloaded according to the arguments passed to them.

An interesting Scenario

Qs:

If, when defining a class, we don't define its constructor, the compiler implicitly assigns one. However, if we do define a constructor, the compiler interprets this as function overriding and does not flash an error. But if this is the case, why do I receive a compiler error in example 3 below?

Example 1:

class x
{
   int x,y;
   public void x( ) // The compiler considers this method overriding.
   {
   }
}
public static void main(String arg[])
{
   x x1=new x() // No error.
}

Example 2:

class x
{
   int x,y;
   public x( ) // The compiler considers this method overloading.
   {
   }
  public x( int a, int b ) // The compiler considers this method overloading.
   {
       x = a;
       y= b;
   }
}
public static void main(String arg[])
{
   x x1=new x() // No error
  x x1=new x( 5, 6 ) // No error
}

Example 3:

class x
{
   int x,y;
   public x( int a, int b )
   {
       x = a;
       y= b;
   }
}
public static void main(String arg[])
{
   x x1=new x() // Why is there an error here?
  x x1=new x( 5, 6 ) // No error
}

Ans:

The behavior that you point out is correct and expected. To understand the behavior, you need to understand a few rules about constructors.

First, if you do not define any constructors, Java will insert a default no-arguments constructor of the form:

public \()
{
   super();
}

Second, if you declare constructors, what you see is what you get. This means that if you don't explicitly declare a no-arguments constructor but declare some other constructor, you will only get the constructor that you do explicitly define. This is why you see an error in example 3. Since you've never declared a no-arguments constructor but did define another type of constructor, Java does not insert a no-arguments constructor.

For example 3 to work you must also explicitly declare the no-arguments constructor:

public x()
{
   super();
}

Finally, subclasses do not inherit constructors. You must explicitly define each constructor that you need. Further, you can only call superconstructors that actually exist in the base class, in accordance with the rules that I outlined above.

this:

When an object’s method needs to refer the object that invoked it the this operator is used to refer that object. this can be used inside any method to refer the current object.

Garbage Collection:

Objects are stored in memory, in C++, dynamically allocated objects must be manually released by use of delete operator. Java de allocates the unused objects from the memory with the help of Garbage collector. The technique in which when no references to an object exists, that object is no longer to be needed and the space is freed is called Garbage Collection. There is no explicit method to destroy un referenced objects, while if necessary you can call the garbage collector else the garbage collector is automatically executed in its low priority thread.

Finalize:

Like in constructors, you initialize object variables or allocate resource to the object, you can perform some actions, or free up resources when the object is being destroyed. Java provides a mechanism called finalization. By using the finalize method you can define specific actions that will occur when an object is just about to reclaimed by the garbage collector. To add a finalizer to a class, you simply define a finalize() method.

The garbage collector runs periodically, checking for objects that are no longer referenced by any running state. Right before the resource is freed, the Java run time calls the finalize method.

The signature for the method is

protected void finalize()

The protected keyword restricts the method to be called by code outside tha class. It is important to note that finalize is just called before the garbage collector destroys it. It is not called when an object goes out of scope.

In C++ destructors also do the same thing except they are executed when an object goes out of scope.

No comments: