Classes

Data members

  • final - value cannot be changed. Must be initialized.
  • static - belongs to the class, rather than to an instance of the class.
  • sychronized - only one object or class can access the data at a time (causes the object to be locked).
  • transient - not a part of the persistent state of an object. Should not be saved and later restored.
  • volatile - informs the compiler that it may be accessed by separate threads asynchronously.

Constants

  • final - use to declare a data member as a constant value.

Global constants

class MyClass {
public static final int x = 10;
}

Static Initializers

static int i;
static { i = 20; } // a block of code
  • Use after the declaration of the static data.

Methods

  • abstract - the method is undefined in the class, and must be defined by any subclass that will be instantiated.
  • final - the method cannot be redefined in a subclass (non-dynamic). The compiler may expand the method (similar to an inline function) if the method is small enough.
  • native - the method links to native machine-dependent code. Declared without a body. Cannot be abstract.
  • static - belongs to the class, rather than to an instance of the class.
  • sychronized - only one object or class can access the method at a time (causes the class to be locked).
  • Can't override an abstract method with another abstract method.
  • A private method can't be abstract.

Constructors

  • When possible, the object should be a valid, meaningful object once it is constructed, as opposed to relying on an Init method.
  • Copy constructor: accepts an object of it's own type as a parameter and copies the data members.
  • The implicit default constructor is only available if no explicit constructors are defined.
  • Constructors can be overloaded.
  • The first statement in a subclass constructor may invoke a superclass constructor: super(...);
  • If there is no explicit call to the superclass constructor, the default superclass constructor will be called.
  • The first statement in a subclass constructor may invoke another subclass constructor: this(...);

Finalize method

  • Performs cleanup when the object goes out of scope; useful for closing resources.
  • The finalize method is called before the garbage collector frees the object; the object is not immediately freed afterwards.
  • There is no guarantee when the finalize method will be called, or the order in which the finalize method will be called for multiple objects.
  • If the interpreter exits without performing garbage collection, the OS may free the objects, in which case the finalize method doesn't get called.
  • protected void finalize() { ... }
  • The finalize method should always be protected.

Class access

  • public - accessible outside the package in which it's defined.
  • default - accessible only within the package in which it's defined.

Member access

  • public - accessible by any class.
  • protected - accessible within the class and by any derived class.
  • private - accessible only within the class.
  • default - accessible by any class within the package.
  • Can't override a public method to protected or private (can't make it more private than it already is).
  • Can override a protected method to public.
  • Can't override a private or final method (causes a private method to be redefined in the subclass).

Inheritance

class ChildClass extends ParentClass { ... }  // ChildClass inherits from ParentClass
  • The default parent of a class is class Object.
  • A class can only extend a single parent class (no multiple inheritance).

Abstract classes

abstract class ClassA {
abstract ... functName(...);
...
}
  • A class is abstract if it contains one or more abstract methods.
  • Abstract classes cannot be instantiated.
  • Derived classes can be instantiated if they define a method for any abstract methods inherited.

Scope

  • this - reference to the current subclass (assumed by default) (i.e. this.someMethod()).
  • super - reference to the parent class (i.e. super.someMethod()).

Misc

  • All methods are implicitly virtual, and can be redefined in a child class, unless they are declared as final.
  • Clone() method allocates and copies (bit-for-bit) a new object. Returns the object as class Object. The class must implement the empty interface Cloneable. The default clone performs a shallow copy, which is not safe in many cases.

Resources URL: 
notes/java/resources
Sources URL: 
notes/java/sources

See Also