This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Instantiating new object within a class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Instantiating new object within a class" Watch "Instantiating new object within a class" New topic
Author

Instantiating new object within a class

Ozzy Boshi
Greenhorn

Joined: Sep 21, 2012
Posts: 7
Hi Javaranch forum users,

I am a java beginner and I have a stupid question about how to instantiate a new object within a class.

I would like to know the difference between this 2 cases:

case 1:

class X {

private class2 z = new class2();

//// CONSTRUCTOR FOR CLASS X
X() {
--- some logic ----
}
}

case 2:
class X {

private class2 z;

//// CONSTRUCTOR FOR CLASS X
X() {
this.z=new class2();
--- some logic ----
}
}

In the first case , when does java instantiate z?at what stage? Before or After that the X() constructor is executed?
What's the difference between this 2 cases and wich is the most correct behaviour?


Thanks in advance to anyone who will answer.
Ozzy
sai rama krishna
Ranch Hand

Joined: May 29, 2009
Posts: 132
I believe
case 1

before X() constructor



case 2 ater


X() constructor
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5845
    
    5

Hi, and welcome to the Ranch!

For future reference, you'll find that your code is easier to read if you UseCodeTags(⇐click) which in turn makes it easier for people to help you.

As for your question, when you initialize the variable at declaration, that is executed when a new object is created after the parent class's constructor has run and before any current class's constructor. It's preferred to to do it there if will be the same regardless of which constructor is invoked, and if everything you need is available at that point.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5845
    
    5

sai rama krishna wrote:I believe
case 1

before X() constructor


Correct.


case 2 ater


X() constructor


No, it's during the X() constructor. We can tell that because that code is inside the X() constructor.
Ozzy Boshi
Greenhorn

Joined: Sep 21, 2012
Posts: 7
Thank you Jeff and sai rama
First of all I apologize for missing the code tag, in the future it won't happen again.
Your answers are clear, all the initialization operations declared in the instance variables likely happen before the constructor is invoked, no matter if they refere to private protected or public variables.
Now I am wondering if there are some common behaviours for using the first example rather than the second.
I am thinking that if I had a class with a big amount of constructors it would be easier to declare the new 'z' instance once in the instance variables, otherwise i should replicate the same code throughout all constructors.

I am sorry for my English, I am not a native english speaker.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5845
    
    5

Ozzy Boshi wrote:
Now I am wondering if there are some common behaviours for using the first example rather than the second.


In my first response, I mentioned the cases to use the first approach. Basically, whenever you can do it, as long as you want to do the same thing for all constructors.

I am thinking that if I had a class with a big amount of constructors it would be easier to declare the new 'z' instance once in the instance variables, otherwise i should replicate the same code throughout all constructors.


You should avoid duplicating code as much as possible. Constructors can call each other, and having a constructor with fewer parameters call one with more parameters, passing default values for the missing parameters, is a very common idiom. Another approach is to have your constructors call a method that executes the repeated code, although this is less common. If you do this, the method should be private or final.

I am sorry for my English, I am not a native english speaker.


No worries. Your English is fine.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Instantiating new object within a class
 
Similar Threads
Creating an instance of a sub-class runs parent and child constructors?
What is a differance here?
inner anonymous class fields initialization
Invoking methods within the same class and from different classes
Six java questions: Could anyone answer