title: Java Homework Help (Accessing Static Member via Instance Reference)
body: <p>Here is my homework question: </p>
  
  <hr>
  
  <p>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:</p>
  
  <p>The time is 3:45:00</p>
  
  <p>// don’t worry if you can’t get both zeros in </p>
  
  <p>// the second field. That is a formatting issue </p>
  
  <p>// we will deal with later</p>
  
  <hr>
  
  <p>Here is my Clock class: </p>
  
  <pre><code>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
  </code></pre>
  
  <p>And here is my ClockDriver class: </p>
  
  <pre><code>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
  </code></pre>
  
  <p>Even though it compiles fine and works nicely, I get what I think is a warning from IDE saying that my </p>
  
  <pre><code>return temp.format("%02d:%02d:%02d", hours, minutes, seconds);
  </code></pre>
  
  <p>line is accessing a static member via instance reference. Specifically, the </p>
  
  <pre><code>temp.format
  </code></pre>
  
  <p>bit.</p>
  
  <p>So my questions are: </p>
  
  <p>1.) Why is accessing a static member via instance reference not necessarily encouraged?</p>
  
  <p>2.) Is there a better way to put this together so that I'm not accessing a static member via instance reference?</p>
  
  <p>Thanks in advance!</p>