J2EEOnline J2EE






Java Certification  «Prev 

Static subclass extending a static parent

You can define a class as a static member inside another class.
The following example demonstrates how a static subclass can extend a parent class that is also declared as static, provided both are contained within a regular class.
In this case 1) Sub and 2) Super are static nested classes of the RegularClass.
static class Sub extends Super and compiles.
package com.java.nestedclasses;
public class RegularClass {
 static class Super {
  void myMethod() throws Exception {
   throw new Exception("throw exception within static parent..");
  }
 }
 static class Sub extends Super {
  void myMethod() {
   System.out.println("My method");
  }
 }
 public static void main(String[] args) {
  new Sub().myMethod();
 }
}
/* 
Program Output 
My method
*/
Member class declarations describe nested classes that are members of the surrounding class. Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes .
Method declarations describe code that may be invoked by method invocation expressions. A class method is invoked relative to the class type; an instance method is invoked with respect to some particular object that is an instance of a class type. A method whose declaration does not indicate how it is implemented must be declared abstract. A method may be declared final, in which case it cannot be hidden or overridden. A method may be implemented by platform-dependent native code.
A synchronized method automatically locks an object before executing its body and automatically unlocks the object on return, as if by use of a synchronized statement , thus allowing its activities to be synchronized with those of other threads.