are constructors really constructing?
Forum » SCJP2 Discussion / constructors » are constructors really constructing?
Started by: itsraghzitsraghz
On: 1201182798|%e %b %Y, %H:%M %Z|agohover
Number of posts: 3
rss icon RSS: New posts
Summary:
constructors are NOT just to construct. they infact MODIFY or UPDATE the instance values!!
are constructors really constructing?
itsraghzitsraghz 1201182798|%e %b %Y, %H:%M %Z|agohover

Reply from Mike Gershman:

In my view, constructors are misnamed - they alter already initialized variables in already created objects, based on the arguments passed to the constructors in the new syntax and on other sources.

Memory allocation for an object is done by the JVM based on information in the class object. The JVM also initializes the instance variables to their default values. Next, the variable initializers and the initialization blocks are executed together in the order they appear in the source code. They can alter the values of the variables. Finally, the constructors get a chance to modify the variables once again. At that point, the object is considered fully initialized and final variables can no longer be altered.

I have not considered the implications of super() here.

BTW, constructors are not methods. Unlike methods, constructors can alter final variables. There are constraints on constructors that don't apply to methods. And you can't call a constructor directly.

unfold are constructors really constructing? by itsraghzitsraghz, 1201182798|%e %b %Y, %H:%M %Z|agohover
Re: are constructors really constructing?
itsraghzitsraghz 1201182899|%e %b %Y, %H:%M %Z|agohover

A Sample program from Ilja Preuss, Sheriff and Author:

public class Foo {
 
  private int foo = 42;
 
  {
    this.print();
  }
 
  public Foo() {
    System.out.println("constructor");
  }
 
  private void print() {
    System.out.println(foo);
  }
 
  public static void main(String[] args) {
    new Foo();
  }
 
}

The following is the output when you run the above program

42
constructor
unfold Re: are constructors really constructing? by itsraghzitsraghz, 1201182899|%e %b %Y, %H:%M %Z|agohover
Re: are constructors really constructing?
itsraghzitsraghz 1201183076|%e %b %Y, %H:%M %Z|agohover

Raghs:

I have tested this in a small program:

public class AreConstructorsModifying {
 
    /* This is to show that instance variables are assigned their default values. 
        See it in printA()'s output. That's why 'a' is not initalized. */
    int a; 
    final int finalA;
    {
        System.out.println("Instance Initialization Block 1");
        //finalA = 1;
        printA();
    }
 
    public void printA() {
        System.out.println("printA(), a = "+this.a);
        //finalA = 2;
    }
 
    /** Creates a new instance of AreConstructorsModifying */
    public AreConstructorsModifying() {
        a = 7;
        finalA = 9;
    }
 
    public String toString() {
        return "[AreConstructorsModifying] : a = "+this.a+", finalA = "+finalA;
    }
 
    public static void main(String[] args) {
        AreConstructorsModifying obj = new AreConstructorsModifying();
        System.out.println("obj is : "+obj);
    }    
}

The output is :

Instance Initialization Block 1
printA(), a = 0
obj is : [AreConstructorsModifying] : a = 7, finalA = 9
last edited on 1201184182|%e %b %Y, %H:%M %Z|agohover by itsraghz + show more
unfold Re: are constructors really constructing? by itsraghzitsraghz, 1201183076|%e %b %Y, %H:%M %Z|agohover
New post
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.