This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Not sure why there is a line in this code... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not sure why there is a line in this code..." Watch "Not sure why there is a line in this code..." New topic
Author

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

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not sure why there is a line in this code...