• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sybex OCA Java 8 Study Guide: page 200 default errata?  RSS feed

 
Charles O'Leary
Ranch Hand
Posts: 175
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sybex page 200 says that the first object created is supposed to have

...the default weight and color.  It then constructs a different object with the desired weight and color...


(I get that this particular Sybex section is about why you really want to be using this(), but presumably, understanding defaults is also essential on the exam.)

Since both objects have the same result:
1) What is meant by the term "default" above?
2) Was the book's expectation that the properties of these 2 ignored objects be different possibly?



output:
Inside public Hamster(int weight,String color) this.weight = 5 this.color = brown
Inside public Hamster(int weight) h.weight = 5 h.color = brown

 
João Victor Gomes
Ranch Hand
Posts: 85
8
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Charles, how are you?

I'm looking at the book here, and you are talking about the following constructors given in the book

The explanation is about when, for example, new Hamster(10) is called, this constructor will create an object with default values for its instance variables. So, the weight and the color of the object created by calling the first constructor (the one with one parameter) will be initialized with the default values: for a String, null, and for an int, 0. So, the object created by calling new Hamster(10) will have null for its color, and 0 for its size (because we don't initialize them).
Then, when the book says "It then constructs a different object with the desired weight and color and ignores the new object", it's talking about the other constructor call, new Hamster(weight, "brown");
Inside the first constructor, we are calling another constructor, and this will construct another object, initializing its values with the desired weight and color passed in the constructor call, since we assign those values to the instance variables, as in line 1 and line 2. This new object is created, but it is ignored, since we call new Hamster(weight, "brown") and never assign it to a variable, and never use it.
 
Charles O'Leary
Ranch Hand
Posts: 175
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi João, It's nice to hear from you again.

Please see both the full compilation and the full execution in my original post. 
 
Charles O'Leary
Ranch Hand
Posts: 175
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More specifically I am asking if "default" in this context is an error of the book ... not in my understanding. Succinctly, instance defaults do not apply.
 
Roel De Nijs
Sheriff
Posts: 11095
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles O'Leary wrote:Sybex page 200 says that the first object created is supposed to have

...the default weight and color.  It then constructs a different object with the desired weight and color...

The book is absolutely spot-on! And with a little tweak to your code snippet, you'll notice that straight awayIf you run your code again now, the output will be:
Inside public Hamster(int weight,String color) this.weight = 5 this.color = brown
Inside public Hamster(int weight) h.weight = 5 h.color = brown
Inside public Hamster(int weight) this.weight = 0 this.color = null


So the statement new Hamster(5); has created two objects: one with the default values ("default" as in the default values which are assigned to instance variables if not initialized with an explicit value) and another object (which goes out of scope once the Hamster(int weight) constructor finishes) with the desired values 5 and "brown". The object created in the main method is the object with the default values. Again illustrated with a small tweak to your code snippetIf you run your code again now, the output will be:
Inside public Hamster(int weight,String color) this.weight = 5 this.color = brown
Inside public Hamster(int weight) h.weight = 5 h.color = brown
Inside public Hamster(int weight) this.weight = 0 this.color = null
Inside main h.weight = 0 h.color = null


Hope it helps!
Kind regards,
Roel
 
Charles O'Leary
Ranch Hand
Posts: 175
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clear! Crystal Clear!    

Thanks Roel and João.  (I figure that was a good take on the whole "How many objects are created?" combined with "What are their respective values?" type questions.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!