-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitializerExamples.java
42 lines (35 loc) · 1.22 KB
/
InitializerExamples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package concept.examples.initializers;
public class InitializerExamples {
static int count;
int i;
{
// This is an instance initializers. Run every time an object is
// created.
// static and instance variables can be accessed
System.out.println("Instance Initializer");
i = 6;
count = count + 1;
System.out
.println("Count when Instance Initializer is run is " + count);
}
static {
// This is a static initializers. Run only when Class is first loaded.
// Only static variables can be accessed
System.out.println("Static Initializer");
// i = 6;//COMPILER ERROR
System.out.println("Count when Static Initializer is run is " + count);
}
public static void main(String[] args) {
InitializerExamples example = new InitializerExamples();// Output below
// Static Initializer
// Count when Static Initializer is run is 0
// Instance Initializer
// Count when Instance Initializer is run is 1
InitializerExamples example1 = new InitializerExamples();// Output below
// Instance Initializer
// Count when Instance Initializer is run is 2
InitializerExamples example2 = new InitializerExamples();// Output below
// Instance Initializer
// Count when Instance Initializer is run is 3
}
}