• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

constructor doubt

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is written in the K & B book "you cannot invoke instance method or variable until after the super constructor has run."
I am not getting it exactly. Can anyone please explain? I am stuck here.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is my understanding:

class Animal{
}

class Horse extends Animal{
int i;
Horse(int i){
this.i=i;
}

Horse(){
this(get());
}

int get(){
return 1;
}
}

There is a compile error because the get() is called before the super class constructor is completed.
[ July 31, 2007: Message edited by: Yeming Hu ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[dolly]:"you cannot invoke instance method or variable until after the super constructor has run."

1- First you need to recall how an object is initialized.
2- How constructor is called, in what order.
3- An object initialization completes when all its super class
constructors have been called. Before that, it have not actually got
the shape of object itself.
Once object takes its form, you can call its methods or use its member
variables. Until all the super constructors have been called, the object
is incomplete.

class Animal{}
class Dog extends Animal{}
class Beagle extends Dog{}

When you create an object to Beagle, first constructor of the Dog
is called, which further calls the constructor of the Animal class and
finally constructor of the Object class is called. (Every object is Java
is an Object with capital 'O').
So when Object class constructor is finished, Animal class constructor
finishes itself, then Dog and finally Beagle class constructor. You see the
that in the form of Stack, how the constructors got called.
When all the constructors have been completed, you have Beagle object in
hand to be used for the purpose of calling methods or using member variables
of the object.


Thanks,
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both of you.
But I am confuse about Constructor chaining which is written in K & B book, chapter-2, page-128. It shows, constructor invoked then instance variable then constructor completes. I am not getting this chaining.
Can you explain please?
 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
Thanks both of you.
But I am confuse about Constructor chaining which is written in K & B book, chapter-2, page-128. It shows, constructor invoked then instance variable then constructor completes. I am not getting this chaining.
Can you explain please?



I am not sure whether I get what you mean, I hope the following code can help you:

class Animal{
int i = getFeet();
String food;
static int getFeet(){
System.out.println("instance variable initialization");
return 1;
}

static String getFood(){
System.out.println("Constructor invoked");
return "Food";
}

Animal(){
this(getFood());
System.out.println("No-arg constructor completes");
}

Animal(String food){
this.food = food;
System.out.println("Arg Constructor completes");
}

public static void main(String[] args){
new Animal();
}
}

Output:
Constructor invoked
instance variable initialization
Arg Constructor completes
No-arg constructor completes
[ July 31, 2007: Message edited by: Yeming Hu ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[dolly]: constructor invoked then instance variable then constructor completes.

I think you are referring to any example where inside the constructor there
is method call(s), so we call a constructor completes itself when all the
instructions/statements of that constructor have been completed.
There is nothing special in the method call from the constructor,
except that method will only be called once super class constructor has
finished its job.


Thanks,
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am a bit confused with the code.
If we uncomment the below lines

int i = getFeet();
static int getFeet(){
System.out.println("instance variable initialization");
return 1;
}

We get the o/p as
Constructor invoked
Arg Constructor completes
No-arg constructor completes

Till this I can understand, but if we uncomment the line we get a different output.
I would like to know, from where the getFeet() method is called.It is only a method and not a static block.

Thanks,
Sumi
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


constructor invoked then instance variable then constructor completes



A Constructor is mainly used to "instantiate" an object. means, not just to create an object in heap but also to initialize the brand new object with some initial values. As that job is also taken care in the constructor, it was told in that order.

  • "constructor invoked" - step 1
  • "instance variable" - step 2 (add a term "initialization" at the end)
  • "constructor completes" - step 3 (normal flow).



  • Hope this clears!
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Originally posted by sumi selva:
    I would like to know, from where the getFeet() method is called. It is only a method and not a static block.



    No. Its a static method.



    When it comes to the initialiazing of superclass through constructor chaining, the instance variable "i" of superclass has to get the value by calling the method "getFeet()" (as per the code). Obviously, you can call ONLY the static method(s) during or within the constructor because to invoke an instance method, there is "NO" object existing and thats what you are doing it now.

    The instance variable "i" is assigned with the value returned by getFeet() method (that is "1" here).

    Does that help you?
     
    Yeming Hu
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Raghavan Muthu:




    When it comes to the initialiazing of superclass through constructor chaining, the instance variable "i" of superclass has to get the value by calling the method "getFeet()" (as per the code). Obviously, you can call ONLY the static method(s) during or within the constructor because to invoke an instance method, there is "NO" object existing and thats what you are doing it now.



    Not necessarily. Before the super class constructor completes, you can't call the instance members. But You can call the instance members during or within its own constructor because the super class constructor already finishes at that time. For my example, i can be assigned a value by a instance method.
    We can refer to Coery's Object Initialization sequence and JLS
     
    dolly shah
    Ranch Hand
    Posts: 383
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeming, Thanks for explanation. But Is this not a method call before supertype constructor has been called?
    Animal(){
    this(getFood());
    System.out.println("No-arg constructor completes");
    }


    Can you explain little more? I am not getting exactly.
     
    Chandra Bhatt
    Ranch Hand
    Posts: 1710
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dolly,

    One programming guideline to you regarding, calling instance method
    from the constructor.

    It is true that, method of a class can be called before the constructor of
    that class has finished itself. There is a big drawback in doing so;
    Suppose you have two classes Animal and Dog. Dog has overridden one method
    of the Animal class. Inside the Animal class constructor you are calling a
    method that has been overridden by class Dog.

    You create an instance of Dog, so first constructor of class Animal will
    complete, and in that you have method call, so polymorphism applies here
    and the Dog version of the method will be called. OK?

    So here what do you see? You see that the method of the Dog class has
    been called even the Dog constructor has not finished its Job; Member
    variables of Dog class has not been initialized and have default value
    even if you assign them values while declaring. So is it OK to call method
    on an object who has not got its shape (member variables have not been
    initialized properly).

    I hope you got the point.
     
    Yeming Hu
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by dolly shah:
    Yeming, Thanks for explanation. But Is this not a method call before supertype constructor has been called?
    Animal(){
    this(getFood());
    System.out.println("No-arg constructor completes");
    }


    Can you explain little more? I am not getting exactly.



    Yes, getFood is called before the super class constructor completes but it's static. If it's instance method, there will be compiler error. Here I just want to show that Constructor is invoked, and then instance variable and then constructor completes which you asked.
     
    dolly shah
    Ranch Hand
    Posts: 383
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot to Yeming & Chandra for such a nice explanation. After suffering a day I got it now.
    I have to say that you guys have good fundamental knowledge.
     
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dude, Following is the Order of calls, Please correct me if I am wrong

    1.static variables super class
    2.Static Blocks - in the order declared - Super Class
    3.static variables sub class
    4.Static Blocks - in the order declared - Sub Class

    5.Instance variables - Super Class
    6.Instance Blocks - in the order declared - Super Class
    7.Super Class Constructor.

    8.Instance variables - Sub Class
    9.Instance Blocks - in the order declared - Sub Class
    10.Sub Class Constructor.
     
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi kishore,

    the initialization of variables and the execution of initialization blocks are no distinct steps.

    1.static variables and static blocks - in the order declared - Super Class
    2.static variables and static blocks - in the order declared - Sub Class

    3.instance variables and instance blocks - in the order declared - Super Class
    4.Super Class Constructor.

    5.instance variables and instance Blocks - in the order declared - Sub Class
    6.Sub Class Constructor.
     
    kishore Kumar Sangam
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The reason for separation of variables from code blocks is say suppose you have the following snippet,

    int j =10;
    {
    int x = j;
    }

    Without initializing the variables the code block cannot be initialized.Thats the reason I feel priority wise instance variables > code blocks.

    Reason : There could be a reference to instance variables from code blocks.
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by kishore kishore:
    Thats the reason I feel priority wise instance variables > code blocks.

    You will find an example, which disprove your feeling, here.
    reply
      Bookmark Topic Watch Topic
    • New Topic