• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 174
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: 3320
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: 174
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
 
Master Rancher
Posts: 3276
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: 19731
92
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: 60155
188
  • 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: 174
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: 60155
188
  • 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.
 
Marshal
Posts: 6008
415
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!