-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeOfVariable.java
More file actions
156 lines (124 loc) · 3.31 KB
/
Copy pathScopeOfVariable.java
File metadata and controls
156 lines (124 loc) · 3.31 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package Phase1_CoreLanguage.Variables;
/**These variables must be declared inside class (outside any function).
* They can be directly accessed anywhere in class. Let’s take a look at an example:
* <p>
*
* We can declare class variables anywhere in class, but outside methods.
* Access specified of member variables doesn’t effect scope of them within a class.
* Member variables can be accessed outside a class with following rules
* <p>
*
* Modifier Package Subclass World
* <p>
*
* public Yes Yes Yes
* <p>
*
* protected Yes Yes No
* <p>
*
* Default (no
* modifier) Yes No No
* <p>
*
* private No No No
* <p>
*
* **/
class GlobalVarible{
// All variables defined directly inside a class
// are member variables
int a;
private String b;
void method1() {}
int method2() {return 0;}
char c;
}
/**Basics.Variables declared inside a method have method level scope and can’t be accessed outside the method.
* this type of variable we can't use outside of the method**/
class LocalVariable {
void method1()
{
// Local variable (Method level scope)
int x;
}
/**
* Local variables don’t exist after method’s execution is over.
* Here’s another example of method scope, except this time the variable got passed in as a parameter to the method
* The below code uses this keyword to differentiate between the local and class variables.**/
private int x;
public void setX(int x)
{
this.x = x;
}
}
class LocalExample
{
static int x = 11;
private int y = 33;
public void method1(int x)
{
LocalExample t = new LocalExample();
this.x = 22;
y = 44;
System.out.println("Test.x: " + LocalExample.x);
System.out.println("t.x: " + t.x);
System.out.println("t.y: " + t.y);
System.out.println("y: " + y);
}
public static void main(String args[])
{
LocalExample t = new LocalExample();
t.method1(5);
}
//OUTPUT
//Test.x: 22
//t.x: 22
//t.y: 33
//y: 44
}
/**
* Loop Basics.Variables (Block Scope)
* A variable declared inside pair of brackets “{” and “}” in a method has scope withing the brackets only.
**/
class LoopVariable {
public static void main(String args[]) {
for (int x = 0; x < 4; x++) {
System.out.println(x);
}
// Will produce error
// Uncomment below line and execute the program
// System.out.println(x);
}
//OUTPUT
//error: cannot find symbol
// System.out.println(x);
public void RightProgram() {
int x;
for (x = 0; x < 5; x++) {
System.out.println(x);
}
System.out.println(x);
//Call This Method, and the OUT is like...
//0
//1
//2
//3
//4
//5
}
//Error One
public void ErrorOne() {
int a = 5;
//uncomment and run the program
// for (int a = 0; a < 5; a++)
// {
// System.out.println(a);
// }
//Error Message is
//error: variable a is already defined in method go(int)
// for (int a = 0; a < 5; a++)
// ^
//1 error
}
}