-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticBlock2.java
More file actions
63 lines (49 loc) · 1.7 KB
/
StaticBlock2.java
File metadata and controls
63 lines (49 loc) · 1.7 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//static block in java
//https://youtu.be/_7q4kMfJPDw?si=hwclbVtm1atyboA7
class Mobile{
String brand;
int price;
static String name;
//static block
//runs only once for n object creations!
//The static block executes even before object creation because static blocks run when the class is loaded into memory,
//not when an instance (object) is created.
//The static block runs only once when the class is first loaded.
static{
name = "Phone";
System.out.println("In static block, i got executed!");
}
//constructor
public Mobile(){
brand = "";
price = 200;
System.out.println("In constructor...");
}
void show(){
System.out.println(brand+" "+price+" "+name);
}
}
public class StaticBlock2{
public static void main(String args[]) throws ClassNotFoundException{
//everytime we create obj, 2 steps are happening in background!
//1. load class
//2. obj is instantiated
//when class load, static vars and methods are instantiated! (Happens only once!)
// Mobile obj1 = new Mobile();
// obj1.brand = "Samsung";
// obj1.price = 500;
// Mobile obj2 = new Mobile();
// obj2.brand = "Apple";
// obj2.price = 1700;
// System.out.println(obj1.name);
// System.out.println(obj2.name);
//Thus
//Static blocks execute when the class is loaded (only once).
//Constructors execute every time an object is created.
//Static variables (name) are shared across all objects.
//if we dont create obj, then no class will be loaded and no static stuff will happen!
//to manually load class without creating instance, use this:
Class.forName("Mobile"); //manually load a class into class loader inside JVM!
//when doing this, exception handling is must! ClassNotFoundException
}
}