• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 499
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 499
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 499
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 499
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic