-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_method.java
35 lines (30 loc) · 989 Bytes
/
static_method.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
class mobile{
String Brand;
int price;
static String name;
void show(){
System.out.println(" Brand: "+Brand+"\nPrice: "+price+"\nName: "+name+"\n");
}
static void show1(mobile obj){
System.out.println("In static method");
System.out.println("Brand: "+obj.Brand+"\nPrice: "+obj.price+"\nName: "+obj.name+"\n");
// cannot use non-static var in static method
}
}
public class static_method {
public static void main(String[] args) {
mobile m1 = new mobile();
m1.Brand = "Android";
m1.price = 12000;
m1.name = "Vivo";
mobile m2 = new mobile();
m2.Brand = "Apple";
m2.price = 120000;
m2.name = "Apple";
mobile.name = "Phone"; //Cannot make a static reference to the non-static field mobile
m1.show();
m2.show();
mobile.show1(m1); // we cannot access mobile.show(), for object m1
mobile.show1(m2); // for object m2
}
}