final class MyClass { public final float pi = 3.1415926 f; public final void doIt ( ) { System.out.println ( "Done !" ); } public void process ( final Customer customer ) { // not allowed // customer.increaseLimit( 1000 ); // customer = new Customer(); } ...... }
public class TestClass { // constant public final static float pi = 3.1415926f; // static data member private static int count = 0; // usual data member private String name; // constructor public TestClass ( String name ) { this.name = name; count ++; } // a usual method String getName ( ) { return name; } // a static method public static int GetCount ( ) { // not allowed // System.out.println ( getName() ); // this is ok return count; } } public class TestClassApp { public static void main ( String args[ ] ) { System.out.println( StaticTest.pi ); // output 3.1415926 Static v ; v = new TestClass ( "AAAAA" ); v = new TestClass ( "BBBBB" ); System.out.println( TestClass.getCount() ); // output 2 } }
class MyClass { void doIt () { System.out.println ( "It is done !" ); } public static void main ( String args[ ] ) { doIt(); } }
public interface MyInterface { int x = 0; // considered as static and final void f(); // considered as abstract }
public interface InterfaceA { int x = 0; // considered as static and final void f( ); // considered as abstract } public interface InterfaceB { int x = 1; // considered as static and final int y = 0; // considered as static and final void g( ); // considered as abstract } public class ClassX implements InterfaceA, InterfaceB { void f( ) { // implement the only abstract method in InterfaceA ...... } void g( ) { // implement the only abstract method in InterfaceB ...... } } InterfaceA InterfaceB ^ ^ . . . . . . . . implements . . implements . . . . . . . . . . ClassX
public class ClassX extends Parent implements InterfaceA, InterfaceB { ...... } ParentClass InterfaceA InterfaceB \ ^ ^ \ . . . . \ . . . . \ . . . . \ . . . . inherit \ . implements . implements \ . . . \ . . . . \ . . . . \ . . . . \. . . . ClassX ClassY
void method1 ( InterfaceA a ) { a.f( ); } void method2 ( InterfaceB b ) { b.g( ); } ClassX v = new ClassX(); method1 ( v ); // the f( ) method of ClassX invoked method2 ( v ); // the g( ) method of ClassX invoked v = new ClassY(); method1 ( v ); // the f( ) method of ClassY invoked method2 ( v ); // the g( ) method of ClassY invoked