• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I make a no-arg constructor create an object with predefined instance variable values?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code. In it, I am able to use overloaded constructors with arguments and create objects which have different states.
However, I read in Head First Java that I can have the default constructor create objects with pre-defined values in their instance variables.
For example, a default constructor can be used to create a car object which always has 20 liters of fuel. How can I do this in my code?

 
Tony Docherty
Bartender
Posts: 3264
81
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of your class you could use the default constructor to create an object with a specific speed and/or weight. You do it by hard coding the value(s) in the constructor ie
 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:In the case of your class you could use the default constructor to create an object with a specific speed and/or weight. You do it by hard coding the value(s) in the constructor ie


Ah! That was simple enough. Thank you
 
Norm Radder
Ranch Foreman
Posts: 2222
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or give the variables default values where they are defined:
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Or give the variables default values where they are defined:


Fun fact: instance-defined values are scooped up by the compiler and placed in an invisible method (some decompilers call it "_init()") which is invoked automatically as part of the construction process. You can actually code entire statement blocks as initializer values.

The "final" qualifier can be applied to such variables, which mirrors the "const" qualifier for C/C++. Something that isn't quite so obvious, however, is that you can declare a member value as "final" and initialize it in constructor code. That's one of the reason's it's "final" and not "const". Once the constructor has initialized the value, however, that value is immutable for the life of the object. You cannot set a "final" property value in a non-constructor method or in external code.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:. . . I read in Head First Java that I can have the default constructor create objects with pre-defined values in their instance variables. . . .
I am quite sure Kathy and Bert didn't call them default constructors. You only get a default constructor if you don't write your own constructor at all. If you look in this link about the javadoc tool, you will find many people think a default constructor is a bad thing.
 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mark Richardson wrote:. . . I read in Head First Java that I can have the default constructor create objects with pre-defined values in their instance variables. . . .
I am quite sure Kathy and Bert didn't call them default constructors. You only get a default constructor if you don't write your own constructor at all. If you look in this link about the javadoc tool, you will find many people think a default constructor is a bad thing.


If you look at page 245-246 of their book.

http://imgur.com/a/1AwC5
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About ten minutes ago, I wrote:I am quite sure Kathy and Bert didn't call them default constructors. . . .
It doesn't say anything about default constructors on that page; it says default size, which is completely different. It also gives an example you could copy for a default weight field.
 
Liutauras Vilda
Marshal
Posts: 4506
306
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:If you look at page 245-246 of their book

Campbell was right. Book is correct. It doesnt say anything about the default constructor. We knew this book is very good.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!