• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors (Java OCA 8 Programmer I Study Guide, Sybex)

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I have a question about some code found in the OCA book 1z0-808, 226 question 21 to be precise.
Here is the code:

Rather than the answer, I'm more interested to the code itself. Just so you know though, my answer was wrong, and the reason is that this really confused me and I thought it was illegal to have that syntax. Needless to say, I've never ever sen anything like that before, and to be fair, I don't even know what that means. Granted, new Cheetah(50) is creating an new object of type Cheetah passing 50 as parameter that is used to initialize numSpots. But what I don't get, and I couldn't find it in the book, is why it is followed by .numSpots. TO me it makes absolutely no sense. Having just this is perfectly fine so why having ? What does that even mean?
thanks

 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:TO me it makes absolutely no sense. Having just this is perfectly fine so why having ? What does that even mean?


Well, the first code creates a new instance -- that part you got. okay.

The second code uses the instance. This is unusual because, normally, you would assign it to a reference, and then use that reference -- in this case, to access the numSpots variable, and pass it to the println() method. Simply, the assignment to the reference is not done, and the object is used as is.

Of course, the next questions is... if it is not assigned to a reference, how will I use it again? Well, as written, you can't. After the instance is dereferenced for the field, it is no longer reachable, and hence, eligible for garbage collection.

Henry

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right, so what would an equivalent way be? Something like this:
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right.
Now you can use the reference "myCheetah" even after your print statement, but within the scope.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:What does that even mean?

The statementis short-hand syntax for the more verboseAnd this code would also be functionally equivalentYou have never ever seen such a statement and you probably never will You will very rarely see such code construct with an instance variable, because this code is pretty re-useless (= useless over and over again ). But you might see a similar code construct with methods, which is called method chaining. Here is a small illustrative code snippet. What's the output?

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Something like this:

Please note that line2 of your code snippet will give a compiler error as it is not a valid statement. Should be something likeor
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic