SE450: Basics: Object Classes and Static Classes [27/63] Previous pageContentsNext page

Let's distinguish two types of classes:

file:Main1.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
package basics.objectclass;
// A static class
public class Main1 {
  private Main1() {}
  static public void main (String[] args) {
    //stdlib.Trace.graphvizShowSteps (true); stdlib.Trace.run ();
    Circle1 c = new Circle1(1);
    String s = ((c==null) ? "null" : c.toString());
    System.out.println(s);
  }
}
// An object class
final class Circle1 extends Object {
  final int radius;
  public Circle1(int radius) { super(); this.radius = radius; }
  public String toString() { return "Circle(" + radius + ")"; }
}

Java allows some shorthand.

The following declaration is equivalent to the one above.

file:Main2.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
package basics.objectclass;
// A static class
public class Main2 {
  private Main2() {}
  static public void main (String[] args) {
    //stdlib.Trace.graphvizShowSteps (true); stdlib.Trace.run ();
    System.out.println(new Circle2 (1));
  }
}
// An object class
final class Circle2 extends Object {
  int radius;
  public Circle2(int radius) { this.radius = radius; }
  public String toString() { return "Circle(" + radius + ")"; }
}

Important: compilers add/remove local variables as they wish (that are not parameters). Some locals get mapped to addresses on the stack and some to registers.

Do not optimize local variables for performance!

Previous pageContentsNext page