Monday, December 24, 2007

Static Members: Invocation Criteria.

public class Test {
public static void method(){ System.out.println("Called"); }
public static void main(String[] args) {
Test t4 = null;
t4.method();
}
}

what will be output of this?
No NullPointer Exception : Static methods and variables use class reference to get invoked.
(not the object even it is initialized) (parsing time search)

Immutable Class: how to create

Mutable Class: State of the object can be changed.
Immutable Class: State of the object cannot be changed.

Steps to declare a class Immutable
1) Declare all the variables final. : State cant be chaaged.
2) Declare all the methods final. : To prevent subclasses from changing the class variables.
3) Pass the values for all the properties in constructor. : To initialize the variables by constructor.(One time setting).

4) At the most declare the class as final.(Not Necessary)