SCJP – Quick Notes

GARBAGE COLLECTION BEHAVIOR

  • The Java garbage collector consists of three basic activities:
    1. Monitors program objects to determine when they are no longer required
    2. Informs selected objects that they should release any non-memory resources
    3. Destroys objects and reclaims their memory resources
  • the gc operates as a seperate asynchronous background thread that tracks all program objects
  • you can request garbage collection by calling one of

Runtime.getRuntime().gc() // no guarantee gc will run

System.gc() // no guarantee gc will run

  • you can also request that the finalize() method be run for objects deemed eligible for collection but which have not yet had their finalization code run

Runtime.runFinalization()

System.runFinalization()

  • Once an Object reference has been set to null it is eligible for garbage collection.
  • every class inherits the finalize() method from java.lang.Object
  • the method is called by the garbage collector when it determines no more references to the object exist

OVERLOADING AND OVERRIDING

  • Java uses late-binding to support polymorphism; which means the decision as to which of the many methods should be used is deferred until runtime
  • Late-binding also supports overriding
  • The access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method
  • The throws clause of the overriding method may only include exceptions that can be thrown by the superclass method, including it’s subclasses
  • A subclass method can overload a superclass method
  • You can overload constructors within the same class
  • You can’t overload them in subclasses as they must have the same name as the class (ie they would have to have the superclass name and would therefore not be constructors in the subclass)
  • You can have multiple overloaded methods in a class but only one overriding method
  • Any class can override methods from its superclass to declare them abstract , turning a concrete method into an abstract one at that point in the type tree. Useful when a class’s default implementation is invalid for part of the class hierarchy
  • Only accessible non-static methods can be overridden

INNER CLASSES

  • Inner classes are non-static classes defined within other classes.

class Outer {

class Inner {} // class definition within the

// the body of class Outer

}

  • The compiled class files for the above are: Outer.class and Outer$Inner.class
  • The Inner class type is: Outer.Inner
  • Instances of inner classes can be created in a number of ways

Create an Outer class object:

Outer o1 = new Outer();

Then create an Inner class object:

Outer.Inner i1 = o1.new Inner();

Or, create the inner class directly:

Outer.Inner i2 = new Outer().new Inner();

Or, create one from within the outer class constructor

class Outer {

Outer() { new Inner(); }

}

  • Inner classes may not declare static initializers or static members unless they are compile time constants ie static final var = value
  • You cannot declare an interface as a member of an inner class; interfaces are never inner
  • The inner class can access the variables and methods declared in the outer class
  • To refer to a field or method in the outer class instance from within the inner class, use Outer.this.fldname

class Outer {

void display() {

class Local {

// body of Local class

}

}

}

  • The compiled name of the above Local class is: Outer$1$Local.class
  • Local classes (above) may not be declared private, public, protected, or static May be declared final

ANONYMOUS CLASSES

  • Anonymous classes are classes which have no name
  • Anonymous classes are declared and defined using the name of the class or interface they extend ie new Enumeration()
  • No modifiers extends or implements are allowed
  • Anonymous classes do not have constructors of their own as constructors always take the name of the class and Anonymous classes have no name
  • even though you cannot use an extends clause, you can extend the superclass by overriding methods
  • you cannot ‘overload’ or ‘add’ new methods

Page Visitors: 2753

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *