• 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

Sierra & Bates self-test question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope this is the place to ask this.
In Chapter 3, self-test question 18 -- I have never seen this before. Method "Two fix()" is defined in class PassO, but looks like a method of class Two! Obviously this works, though I have no idea how. Any help understanding this would be appreciated.

[Dan added the UBB codes to format the code block.]
[ March 23, 2003: Message edited by: Dan Chisholm ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Doug Patterson:
I hope this is the place to ask this.
In Chapter 3, self-test question 18 -- I have never seen this before. Method "Two fix()" is defined in class PassO, but looks like a method of class Two!


Doug,
Welcome to the Java Ranch!
I'm not sure that I understand your question but I will try to explain how the code works.
The start method creates an instance of class Two and then prints the default value of t.x. The default value is zero.
The reference to the instance, t, is passed to the fix method where the value of t is changed to 42. When the method returns both references, t and t2, refer to the same instance so the value 42 is printed twice.
I hope that helps.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And a little bit more clarification...
fix( ) is a method in class PassO, that RETURNS an object of type Two...
does that help?

These questions are similar to what you'll see on the real exam, confusing names and code - not my style when I'm writing code, but our hope is that it will get you properly prepared for the exam
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic