• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Constructors in Java

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both are having a different usage and both are correct
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 14674
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@You Gin
You are welcome.
 
Stephan van Hulst
Saloon Keeper
Posts: 14674
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic