forked from Nabin-joshi/java_notes_and_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticMethod.java
56 lines (45 loc) · 1.53 KB
/
StaticMethod.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class outsideClass{
String brand;
int price;
static String name;
// static block bhitra static variable or method matrai use gafrna painxa
static {
System.out.println("hello");
name="phone";
}
// If we change the value of the static variable inside the constructor all the other methods that are implementing the static variable will change their value
public outsideClass(){
brand="";
price=200;
name="nabin";
System.out.println(name+price);
}
public void outsideclass(){
brand="";
price=200;
System.out.println(price);
System.out.println(name);
}
// this is how non static variable can be accessed in static method
static void method(outsideClass obj){
System.out.println("hello "+ name + obj.price);
}
}
public class StaticMethod {
public static void main(String[] args) throws ClassNotFoundException {
// but the best part is when the class is loaded ststic block runs
// Hence static will run first then instance then constructor then method
// this is used for loading the class without creating the object
// try {
// Class.forName("outsideClass");
// } catch (ClassNotFoundException e) {
// throw new RuntimeException(e);
// }
outsideClass obj1= new outsideClass();
// outsideClass.method();
obj1.outsideclass();
obj1.brand="Samsung";
obj1.price=400;
outsideClass.method(obj1);
}
}