• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not sure why there is a line in this code...

 
Jake Miller
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code:


It's entirely possible I don't understand the setTime method with the void (this just means that it performs the method without spitting out a value, right?), but I am confused about why they do "time = t." They have already established time as a string, and then they copy it to "t" but don't seem to do much with it. What's going on?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jake,

This program doesn't do anything useful, so I understand why you don't see a reason for what's going on. What's happening with the setTime() method is that some other code can create a Clock, call setTime() to tell it what time it is, and then that Clock remembers that time value, forever.

Besides the fact that there's no immediate use for this, there's also the glaring problem that clocks don't, in fact, remember one time value forever; they have a constantly-changing time value.Let's set that aside, however. Imagine that this class represents a broken clock, the kind that's only right twice a day. You can set it, but then it doesn't change.

So (trivial example, but hopefully it makes a little more sense) imagine that Clock has a method called displayTime(). It might look like this:



Now I can create any number of broken clocks, each stuck at a different time, and ask each one to describe its own unique perspective:



Imagine (if your imagination isn't already overworked!) that this class is part of a text adventure computer game. Every time you look at a clock, you want it to display its little message. There are several broken clocks in the game, and each broken clock remembers the time you set it to. Woooo.

Now, how do we make this useful? Imagine a Report class, with setTitle(), setNumColumns(), setData(), setLogo()... then you build a Report, and then call Report.preview(). It if looks good, you call report.print().

This general idea, with objects that remember their own state, is extremely important in Java.
 
Jake Miller
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest-
Thanks a lot. So I guess I did understand it (minimally), it's just that that line doesn't really do a whole lot which was confusing me. Your examples are helpful, but a quick follow up. I'm just a little confused about this one:


Now, how do we make this useful? Imagine a Report class, with setTitle(), setNumColumns(), setData(), setLogo()... then you build a Report, and then call Report.preview(). It if looks good, you call report.print().


How does that relate to objects remembering their states?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You create a Report object, and you set its various attributes. It remembers all the values you set. Then at the end, you can display() the report, or print() the report, or email() the report, and any of those methods would use all the stored values. You might create ten reports and then email() them all at once.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Miller: but I am confused about why they do "time = t." They have already established time as a string, and then they copy it to "t" but don't seem to do much with it. What's going on?




The variable "time" is an instance variable. It is visible to all the code within the Clock class.

The variable "t" is local to the setTime method.
The line

assigns the value of 't' to the instance variable 'time'.

The instance variable "time" has been encapsulated.
It can only be accessed or modified via the getTime and setTime methods respectively by methods outside of this class's package.
(Note: In a better example of encapsulation "time" would be declared private.)

See:
http://en.wikipedia.org/wiki/Information_hiding
for more on encapsulation.
[ August 08, 2007: Message edited by: Ben Souther ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic