Let's distinguish two types of classes:
-
object classes are instantiable -- they are
templates for stamping objects
-
static classes are not instantiable -- they
are collections of functions
|
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.
|
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!


