Difference between revisions of "Instantiation"
From Suhrid.net Wiki
Jump to navigationJump to search| Line 53: | Line 53: | ||
* In case of multiple init blocks (either instance and static) they run in the order in which they are defined. | * In case of multiple init blocks (either instance and static) they run in the order in which they are defined. | ||
* Exceptions in init blocks can be handled using try-catch mechanism. | * Exceptions in init blocks can be handled using try-catch mechanism. | ||
| + | |||
* A '''java.lang.ExceptionInInitializerError''' is thrown to indicate that an exception occurred during evaluation of a '''static''' initializer block or the initializer for a static variable. | * A '''java.lang.ExceptionInInitializerError''' is thrown to indicate that an exception occurred during evaluation of a '''static''' initializer block or the initializer for a static variable. | ||
| + | |||
| + | * An instance-init block can result in an uncaught exception - provided that the exception is declared in the throws clause of every constructor. | ||
| + | * In a static - init block this is NOT possible - since there is no constructor invocation to handle the exception. So no uncaught checked exceptions are possible for static initializers. | ||
| + | |||
| + | * Example: | ||
| + | |||
| + | <syntaxhighlight lang="java5"> | ||
| + | |||
| + | public class Init5 { | ||
| + | |||
| + | private int id; | ||
| + | |||
| + | private static int count = 10; | ||
| + | |||
| + | static { | ||
| + | if(count < 20) { | ||
| + | throw new FileNotFoundException(); //Error | ||
| + | } | ||
| + | } | ||
| + | |||
| + | { | ||
| + | if(id > 0) { | ||
| + | throw new InterruptedException(); //Uncaught checked exception allowed, because constructor declares it. | ||
| + | } | ||
| + | } | ||
| + | |||
| + | Init5(int id) throws InterruptedException { | ||
| + | this.id = id; | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | try { | ||
| + | Init5 i0 = new Init5(0); | ||
| + | } catch (InterruptedException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </syntaxhighlight> | ||
[[Category:OCPJP]] | [[Category:OCPJP]] | ||
Revision as of 21:55, 16 August 2011
Constructors
- Every class - including Abstract classes - MUST have a constructor.
- Constructor chaining is built in. Every class constructor will call its default superclass constructor using super().
- Every constructor as its first statement has a call to this() or super().
- Compiler will only automatically insert call to no-arg super() or this().
- CANNOT call an instance method or access instance variables till the super constructor has run.
- Only Static variables can be accessed as a call to super() or this().
Field Initializers
- Initialization of fields can be done through field declaration statements.
- Declaration of field must occur before its usage in any initializer if it is used on the RHS of an assignment. This will always result in a compiler error. Example:
public class Init4 {
int length = 10;
int area = length * width; //Illegal since width is a forward reference being used on the RHS.
int width = 20;
}
- Initializer expression must not result in an uncaught checked exception. Compiler will throw an error.
public class Init4 {
int length = 10;
int width = 20;
int area = getArea(); //Compiler error - uncaught exception.
private int getArea() throws Exception {
return length * width;
}
}
Initialization Blocks
- A static initialization block runs when the class is first loaded.(Runs only once)
- An instance init block runs every time a new instance is created.
- An instance init block runs right after the call to super() in a constructor (ie. after all the super-constructors have run).
- In case of multiple init blocks (either instance and static) they run in the order in which they are defined.
- Exceptions in init blocks can be handled using try-catch mechanism.
- A java.lang.ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer block or the initializer for a static variable.
- An instance-init block can result in an uncaught exception - provided that the exception is declared in the throws clause of every constructor.
- In a static - init block this is NOT possible - since there is no constructor invocation to handle the exception. So no uncaught checked exceptions are possible for static initializers.
- Example:
public class Init5 {
private int id;
private static int count = 10;
static {
if(count < 20) {
throw new FileNotFoundException(); //Error
}
}
{
if(id > 0) {
throw new InterruptedException(); //Uncaught checked exception allowed, because constructor declares it.
}
}
Init5(int id) throws InterruptedException {
this.id = id;
}
public static void main(String[] args) {
try {
Init5 i0 = new Init5(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}