• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating new object within a class  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe
case 1

before X() constructor



case 2 ater


X() constructor
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!