-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariables.java
64 lines (50 loc) · 2.02 KB
/
Variables.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
57
58
59
60
61
62
63
64
public class Variables {
/*public static String myClassVar = "class or static variable";
public static void main(String args[]){
StaticVarExample obj = new StaticVarExample();
StaticVarExample obj2 = new StaticVarExample();
StaticVarExample obj3 = new StaticVarExample();
//All three will display "class or static variable"
System.out.println(obj.myClassVar);
System.out.println(obj2.myClassVar);
System.out.println(obj3.myClassVar);
//changing the value of static variable using obj 2
obj2.myClassVar = "Changed Text";
//All three will display "Changed Text"
System.out.println(obj.myClassVar);
System.out.println(obj2.myClassVar);
System.out.println(obj3.myClassVar);
}*/
/*String myInstanceVar = "instance variable";
public static void main(String args[]){
Variables obj = new Variables();
Variables obj2 = new Variables();
Variables obj3 = new Variables();
System.out.println(obj.myInstanceVar);
System.out.println(obj2.myInstanceVar);
System.out.println(obj3.myInstanceVar);
obj2.myInstanceVar = "Changed Text";
System.out.println(obj.myInstanceVar);
System.out.println(obj2.myInstanceVar);
System.out.println(obj3.myInstanceVar);
}*/
// instance variable
public String myVar = "instance variable";
public void myMethod(){
// local variable
String myVar = "Inside Method";
System.out.println(myVar);
}
public static void main(String args[]){
// Creating object
Variables obj = new Variables();
/* We are calling the method, that changes the
value of myVar. We are displaying myVar again after
the method call, to demonstrate that the local
variable scope is limited to the method itself.
*/
System.out.println("Calling Method");
obj.myMethod();
System.out.println(obj.myVar);
}
}