This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Constructors in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors in Java" Watch "Constructors in Java" New topic
Author

Constructors in Java

You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Hi guys,

I've got an impression that it is a good practice to make your instance variables initialization within class's constructor


At the same time in most examples I see instance variables are initialized like:


So, as far as I understand, by the time new is returning a reference to a newly created object the instance variable are initialized for both these cases.
Put it in a few words, am I right that there is no need to make initialization in Constructor for cases when I'm not defining initial value(s) for instance variable(s)?

Thank you very much.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I'm not quite sure what your question is but both examples are correct and will initialize the values.
I prefer the second one because it looks cleaner to me.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

both are having a different usage and both are correct


SCJP6.0,My blog Ranchers from Delhi
Kshitij Chandrasen
Greenhorn

Joined: Oct 11, 2010
Posts: 8
Initializations in the constructor are generally used for non-static and non-final (obviously) class variables. Both the examples will work correctly, but as a general practice initialization of i should be done in the constructor.

Thanks,
Kshitij
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
Both usages have advantages and disadvantages.

Consider a class with more than one constructor. Using the first variant then you have to set the value of i in each constructor (assuming you are not using this()). Using the second variant then the value of i will be 3 independent of which constructor you use.

I have a preference for the second variant and declare the attribute with a 'default' value that can then be overridden by any constructor that needs to.

However you choose to do it, make sure its well documented.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Why? It should be an implementation detail.

It *shouldn't* be documented, other than some optional lines of simple comment, because you would be committing to an implementation.
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
Are you saying that you should never document default values??? I agree that if the attribute is internal with no way of changing from outside then you don't want to commit to an implementation. With the limited example in the OP there is no way to tell if this is the case or not.

However, it may be relevant information for a client of your API to know which constructor to use. Look at the available constructors of HashMap. Are you saying that the designers got it wrong by documenting the default value for the no arg constructor:
Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

I suppose one alternative in this case may be to force the API client to always use one of the other constructors but that would defeat the purpose of having default values.
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Thanks guys, now I understand this choice where to initialize your variables depends on you and current needs, but both cases are correct.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

@You Gin
You are welcome.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Paul Beckett wrote:Are you saying that you should never document default values??? I agree that if the attribute is internal with no way of changing from outside then you don't want to commit to an implementation. With the limited example in the OP there is no way to tell if this is the case or not.

However, it may be relevant information for a client of your API to know which constructor to use. Look at the available constructors of HashMap. Are you saying that the designers got it wrong by documenting the default value for the no arg constructor:
Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

I suppose one alternative in this case may be to force the API client to always use one of the other constructors but that would defeat the purpose of having default values.


Yes, I think this is exactly an example of a mistake they made. At some point in the future these figures might not even make sense anymore, but they are now committed to these values. There is absolutely no reason why they should have documented them. It's simply a convenience constructor, and most people don't even care about the capacity and load factor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors in Java