This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • 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

 
Ranch Hand
Posts: 166
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?

 
Bartender
Posts: 3315
86
  • 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: 166
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
 
Rancher
Posts: 3061
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or give the variables default values where they are defined:
 
Bartender
Posts: 19447
88
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.
 
Marshal
Posts: 59086
180
  • 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: 166
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
Marshal
Posts: 59086
180
  • 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.
 
Sheriff
Posts: 5686
393
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.
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!