-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticBlocks.java
38 lines (31 loc) · 1.13 KB
/
StaticBlocks.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
class Mobile{
String brand;
int price;
static String name;
// intializing with constructor
Mobile(){
brand="";
name="Phone";
System.out.println("In Constructor ");
}
// static block intialize variables - static blocks are called first
static {
name = "Phone";
System.out.println("In Static Block");
}
public void show(){
System.out.println(brand + " " + price + " " + name);
}
}
public class StaticBlocks {
public static void main(String args[]) throws ClassNotFoundException{
Mobile obj1 = new Mobile();
obj1.brand = "Apple";
obj1.price = 1500;
Mobile.name = "SmartPhone";
// Mobile obj2 = new Mobile();
// everytime - class loads and object are instantiated
// jvm has class loader, only once, static method will be called first, and then objects will be created, so after that constructor will be created
// Class.forName("Mobile");// it loads class name is given as parameter // this given an exception for now , so we handled that at
}
}