24

Here is my homework question:


Write a class declaration for a class “Clock”. It should have instance variables for hours, minutes, seconds (all integers). It should also have a toString() method to show the time in the format shown below. Write a separate “ClockDriver” class to a) create an instance of a clock, b) set the hours, minutes, and seconds for the clock, and c) show the time of the Clock using getTime(). Use the Dog class example on page 36 as a guide. Sample out is shown below:

The time is 3:45:00

// don’t worry if you can’t get both zeros in

// the second field. That is a formatting issue

// we will deal with later


Here is my Clock class:

class Clock  {

int hours;
int minutes;
int seconds;


public String toString() {

    String temp = ("");
    return temp.format("%02d:%02d:%02d", hours, minutes, seconds);

} //end method toString

public void getTime() {

    System.out.print("The time is " + toString());

} //end method getTime

} //end class Clock

And here is my ClockDriver class:

public class ClockDriver {

    public static void main (String[] args) {

        Clock c = new Clock();
        c.hours = 4;
        c.minutes = 30;
        c.seconds = 00;
        c.getTime();

    } //end main

} //end class ClockDriver

Even though it compiles fine and works nicely, I get what I think is a warning from IDE saying that my

return temp.format("%02d:%02d:%02d", hours, minutes, seconds);

line is accessing a static member via instance reference. Specifically, the

temp.format

bit.

So my questions are:

1.) Why is accessing a static member via instance reference not necessarily encouraged?

2.) Is there a better way to put this together so that I'm not accessing a static member via instance reference?

Thanks in advance!

Bit Deception
  • 297
  • 1
  • 3
  • 7

3 Answers3

43

Static methods belong to the class itself, not any instance. While you can call a static method from an instance of the class, you don't have to use an instance of the class, and you should not. It can be confusing, because it looks like the method is not static, even though it is static.

The best and clearest way to call a static method is to use the class name itself, not an instance of the class, to call the method:

return String.format("%02d:%02d:%02d", hours, minutes, seconds);

And you don't need the temp instance at all.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Ok, thank you! I was having problems and my teacher threw a snippet my way that used temp. I guess he wasn't expecting me to figure out how to get the formatting right which is why it didn't work out. – Bit Deception Aug 20 '13 at 21:13
2

Keep in mind that if an instance of a certain Class, lets call it c, has a function, void foo(), and you use the instance c to call foo like so:

c.foo();

what actually called is:

foo(c);

while calling a static function void foo2():

Class.foo2();

is called just as it looks.

Calling a static function using an instance indicates a user that probably is not sure what he is doing.

CaptJak
  • 3,592
  • 1
  • 29
  • 50
TomF
  • 183
  • 11
0

You should actually not use the string temp. You should call it as String.format. To answer your question, it is confusing, since the call actually doesn't look at temp.

tbodt
  • 16,609
  • 6
  • 58
  • 83