The static Keyword The final Keyword

J.E.D.I. super refers to the immediate superclass. It should be the first statement in the subclasss constructor. The keyword can also be used to refer to superclass members as shown in the following example. class Superclass{ int a; void display_a{ System.out.printlna = + a; } } class Subclass extends Superclass { int a; void display_a{ System.out.printlna = + a; } void set_super_aint n{ super.a = n; } void display_super_a{ super.display_a; } } class SuperDemo { public static void mainString args[]{ Superclass SuperObj = new Superclass; Subclass SubObj = new Subclass; SuperObj.a = 1; SubObj.a = 2; SubObj.set_super_a3; SuperObj.display_a; SubObj.display_a; SubObj.display_super_a; System.out.printlnSubObj.a; } } The program displays the following result. a = 1 a = 2 a = 3 2

1.3.16 The static Keyword

The static keyword can be applied to the members of a class. The keyword allows static or class members to be accessed even before any instance of the class is created. A class variable behaves like a global variable. This means that the variable can be accessed by all instances of the class. Class methods may be invoked without creating an object of its class. However, they can only access static members of the class. In addition to this, they cannot refer to this or super. Introduction to Programming II Page 21 J.E.D.I. The static keyword can also be applied to blocks. These are called static blocks. These blocks are executed only once, when the class is loaded. These are usually used to initialize class variables. class Demo { static int a = 0; static void staticMethodint i { System.out.printlni; } static { static block System.out.printlnThis is a static block.; a += 1; } } class StaticDemo { public static void mainString args[] { System.out.printlnDemo.a; Demo.staticMethod5; Demo d = new Demo; System.out.printlnd.a; d.staticMethod0; Demo e = new Demo; System.out.printlne.a; d.a += 3; System.out.printlnDemo.a+, +d.a +, +e.a; } } The output for the source code is shown below. This is a static block. 1 5 1 1 4, 4, 4

1.3.17 The final Keyword

The final keyword can be applied to variables, methods and classes. To remember the function of the keyword, just remember that it simply restricts what we can do with the variables, methods and classes. The value of a final variable can no longer be modified once its value has been set. For example, final int data = 10; The following statement will cause a compilation error to occur: data++; A final method cannot be overridden in the child class. final void myMethod { in a parent class } myMethod can no longer be overridden in the child class. A final class cannot be inherited unlike ordinary classes. final public class MyClass { Introduction to Programming II Page 22 J.E.D.I. } Coding Guidelines: Order of typing the final and public keyword may be interchanged. Having this statement will cause a compilation error to occur since MyClass can no longer be extended. public WrongClass extends MyClass { }

1.3.18 Inner Classes