The Basics of Classes in Jave

In Java all methods must be defined within a class or interface. For example
public class Time
  {
  private int hour;
  private int second;
  private int minute;
  public Time()
    {
    setHour(0);
    setMinute(0);
    setSecond(0);
    }
  public Time(int h, int m, int s)
    {
    setHour(h);
    setMinute(m);
    setSecond(s);
    }
  public void setHour(int h)
    {
    hour=h;
    }
  public void setMinute(int m)
    {
    minute=m;
    }
  public void setSecond(int s)
    {
    second=s;
    }
  public int getHour()
    {
    return hour;
    }
  public int getMinute()
    {
    return minute;
    }
  public int getSecond()
    {
    return second;
    }
  public String toString()
    {
    String temp=new String();
    if( hour<10 )
      temp = temp + "0";
    temp = temp + hour + ":";
    if( minute<10 )
      temp = temp + "0";
    temp = temp + minute + ":";
    if( second<10 )
      temp = temp + "0";
    temp = temp + second;
    return temp;
    }
  }
This example illustrates a number of principles. The variables hour, minute, and
second are called instance variables. An instance of the class Time is when you
use the new operator to instantiate (i.e allocate memory for) a Time object.
Time a;  //a reference
a=new Time(12,23,20);  //an instance of a Time object
Each instance of the class Time has a copy of these instance variables. They have
class scope. Their duration is the duration of the instantiated object itself. That
is to say, they are created when the new operator instantiates the object and destroyed
when the object is garbage collected (the garbarge collector takes care of deleting
memory). Next, notice that the method toString has a local variable called temp. The
duration of this variable is simply the execution of the method itself. We overloaded the
Time constructor so that we can call it without any arguments. Lets use the class
Time in a class with a member method main.
import Time;

public class Driver
  {
  public static void main( String args[] )
    {
    Time a = new Time();
    Time b = new Time(1,34,6);
    System.out.println(a.toString());
    System.out.println(b.toString());
    System.exit(0);
    }
  }
Even the main method must be defined as a member method of a class. The output of this
code is
00:00:00
01:34:06
We didnt really have to import the Time class if the Driver and Time classes were located
in the same directory (i.e. folder).

Since all methods are defined within a class the question of "symbol lookup", that is how
the compiler determines which variable or reference a particular identifier represents,
is very important. The basic rule of thumb is:

  1. The compiler looks in the current scope to see if the identifier is defined
  2. If it isnt, the compiler looks in the outer scope to see if the name is defined
For example
public class Test
  {
  int a=9;
  int b=10;
  public void f( )
    {
    int a=20;
    System.out.println( Integer.toString(a) );
    System.out.println( Integer.toString(b) );
    }
  }
The output of the piece of code
Test x = new Test();
is
20
10
We say that within the scope of the function public void f( ) the local variable
a hides the instance variable a. I'll mention in passing that although I didnt define
a constructor for the class Test, it gets a default constructor that excepts no arguments.

The variable modifiers we have learned so far are:

Lets move on to inheritance in the next notes