public class Circle { ... }
Circle c1 = new Circle(...);
Circle c2 = new Circle(...);
class Shape { ... }
class Circle extends Shape { ... }
interface Drawable { ... }
interface Saveable { ... }
class SuperDuperCircle extends Circle implements Drawable, Saveable { ... }
class Shape {
private Color color; // field
public Shape(Color c) {color = c;} // constructor
static { System.out.println("Shapes!"); } // static initializer
{ System.out.println("Made a shape"); } // instance initializer
public Color getColor() {return color;} // method
}
class C {
int x; // instance field
static int y; // class field (shared among all instances)
}
C a = new C();
C b = new C();
a.x = 1;
b.x = 2;
a.y = 3;
b.y = 4;
// Now a.x == 1, b.x == 2, a.y == 4, b.y == 4, C.y == 4
| Class Modifiers | |
|---|---|
| no modifier | accessible only within its own package |
| public | accessible wherever its package is |
| abstract | cannot be instantiated (may have abstract methods) |
| final | cannot be extended (subclassed) |
| static | makes an inner-declared class really a top-level one |
| Member Modifiers | |
| private | accessible only within its class |
| no modifier | accessible only within its package |
| protected | accessible only within its package and to its subclasses |
| public | accessible wherever its class is |
| Field Modifiers | |
| final | value may not be changed, once assigned |
| static | only one instance of the field shared by all objects of the class |
| transient | field is not serialized |
| volatile | value may change asynchronously compiler must avoid certain optimizations |
| Method Modifiers | |
| final | may not be overridden |
| static | method does not apply to a particular instance |
| abstract | has no body; subclasses will implement |
| synchronized | requires locking the object before execution |
| native | implementation is not written in Java, but rather in some platform dependent way |
There are five kinds of classes: package-level, nested top-level, member, local or anonymous.
/**
* A throw-away class that illustrates the five kinds of Java classes.
* 'A' is a package-level class, 'B' is a nested top-level class,
* 'C' is a local class, 'D' is a local class, and there is an
* anonymous class that is a subclass of Thread.
*/
class A {
int x;
static class B {
int x;
}
class C {
int x;
int sum() {return x + A.this.x;}
}
void f() {
class D {
int x;
}
D d = new D();
Thread e = new Thread() {
public void run() {
System.out.print("Hello from " + getName());
}
};
d.x += 4;
e.start();
}
}
/**
* Application that shows off how one would use the demo class above.
*/
public class ClassExample {
public static void main(String[] args) {
A a = new A();
a.x = 1;
A.B b = new A.B();
b.x = 2;
A.C c = a.new C();
c.x = 3;
System.out.println(c.sum());
a.f();
}
}
class Greeter<V> {
private V target;
...
}
class Pair<T1, T2> {
private T1 first;
private T2 second;
...
}
Greeter<Integer> r1 = new Greeter<Integer>();
Greeter<?> r2 = new Greeter<String>();
Greeter<? extends JComponent> r3 = new Greeter<JCheckBox>();
Greeter<? super Graphics2D> r4 = new Greeter<Graphics>();
public class CollectionUtil {
...
public static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
for (T o : a) {
c.add(o);
}
}
}
Try to figure out what this program does, without running it. After you think you have your answer, run it to see if you are right.
public class T {
private final String name;
private void printName() {
System.out.println(name);
}
public T(String name) {
this.name = name;
}
public static void main(String[] args) {
new T("main").doIt();
}
private void doIt() {
new T("doIt") {
void method() {
printName();
}
}.method();
}
}