aspose file tools*
The moose likes Beginning Java and the fly likes Disadvantages of in-place instantiation of a instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Disadvantages of in-place instantiation of a instance variable" Watch "Disadvantages of in-place instantiation of a instance variable" New topic
Author

Disadvantages of in-place instantiation of a instance variable

Suresh S Nayar
Greenhorn

Joined: Apr 25, 2009
Posts: 9
Assume
class X{
}

What is the difference between

public class A{
X x = new X();
}

and

public class B{
X x;
public B(){
x = new X();
}
}

In case of class A, i understand that due to the way construction takes place, the reference variable x will be initiated before the constructor runs, while in case of class B, the reference variable x will first be set to null and then will be initiated in the constructor.

My question is if there is any particular disadvantage in case of class A. Would this in any way affect class loading for class A. Would an object of type X be created when the class A is first loaded into the JVM.

many thanks.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

Honestly there is no difference except when you are doing Swing GUI stuff which I experienced with.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Probably a stylistic difference; the first method allows you to "forget" to write a constructor, so I prefer the second.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

There a slight difference in the way the constructors of the classes would run. In the first case constructor of class X would run followed by that of A.
In the later the constructor of class B would run first followed by that of X. (at which ever point is actually constructed).

In real life it shouldn't really matter - your code really should not depend on the order of constructors but if it does ...


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

K. Tsang wrote:Honestly there is no difference except when you are doing Swing GUI stuff which I experienced with.


Can you please elaborate on this?
Swing is still java code. So how would it differ in Swing?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Campbell Ritchie wrote:Probably a stylistic difference; the first method allows you to "forget" to write a constructor, so I prefer the second.


I understand that we are talking about individual preferences here, but say if you have three four such member variables and two, three overloaded constructors you either have to call the default constructor (or one of the constructors) from the overloaded ones, or have to repeat the code in all the constructors. If the initialization of some of the members is not dependent on the parameters passed to constructors don't you think the first approach is better for to initialize such members? IMHO it reduces the chances of programmer forgetting to initialize them in one of the constructors.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Moojid Hamid wrote:
Campbell Ritchie wrote:Probably a stylistic difference; the first method allows you to "forget" to write a constructor, so I prefer the second.


I understand that we are talking about individual preferences here, but say if you have three four such member variables and two, three overloaded constructors you either have to call the default constructor (or one of the constructors) from the overloaded ones, or have to repeat the code in all the constructors. If the initialization of some of the members is not dependent on the parameters passed to constructors don't you think the first approach is better for to initialize such members? IMHO it reduces the chances of programmer forgetting to initialize them in one of the constructors.


In such a case you should always end up calling the default constructor which has the initialization code - using constructor chaining ... Most real world code would.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Suppose that the constructor of X might throw an exception. Then with the first style:

You will not have a chance to catch and handle the exception. With the second style, you could add a try-catch block:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Hello all...

This might come across as a dumb question actually...

I understand that the second variant is better due to the error/exception catching capabilities... but what if my class has multiple constructors.... default one, and a lot of parameterized ones too.... wont I have to duplicate this code of try-catch in that case.. ? whereas, if I go for style1, I wont have to bother about it at all....
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

If you have multiple constructors, they should be internally chained.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Agree about chaining, Maneesh, but are you really going to let a null loose like that?

It is fascinating the way this thread has developed, showing all sorts of ideas from a simple start
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Campbell Ritchie wrote:Agree about chaining, Maneesh, but are you really going to let a null loose like that?


Null loose? I'm afraid I don't understand. Can you please rephrase it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Passing a null if you invoke the no-args constructor?
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Campbell Ritchie wrote:Agree about chaining, Maneesh, but are you really going to let a null loose like that?
It is fascinating the way this thread has developed, showing all sorts of ideas from a simple start


I guess what Maneesh posted is just an example of chaining. Making a call this(null) with a null parameter wasn't the best showcase

Usually I end up chaining from a parametrized constructor to the default constructor and not the other way around.

The advantages and disadvantages of each approach notwithstanding - the final approach one takes seems so much a matter of style - at least in 'most' real world code
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Campbell Ritchie wrote:Passing a null if you invoke the no-args constructor?

The no arg constructor is invoked without the null from outside. Internally when it is chained, you have to pass null.

Similar to


PS. I am sure something is wrong with me today. I strongly suspect I am not getting what you are trying to say.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Yes, I think we are misunderstanding each other. What I meant is, do you want any nulls in the first place? If you call the no-arg constructor, you are passing null references where they can infect other code and surprise you with NullPointerExceptions later on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Disadvantages of in-place instantiation of a instance variable