-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_Variables.java
More file actions
47 lines (33 loc) · 1.3 KB
/
06_Variables.java
File metadata and controls
47 lines (33 loc) · 1.3 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
class Solution {
public static void main (String args[]){
int i; // local var
// Error: If we print an uninitialised variable, we get error
// System.out.println(i);
i = 32;
System.out.println(i); // Prints 32:
// public int j = 21; // Gives error: we cannot use access specifiers with
// local variable
}
}
class Solution1 {
// Instance Variables: They have a default value
// Boolean: false, Char: Large val, Int: Large val...
String name; // No error, ever if we dont initialise it.
int k = 21;
// We can create static instance variables to access without class instance
static int j = 22;
public static void main (String args[]){
int i; // local var
// Error: If we print an uninitialised variable, we get error
// System.out.println(i);
i = 32;
System.out.println(i); // Prints 32:
// public int j = 21; // Gives error: we cannot use access specifiers with
// local variable
// To print the instance member we have to use the instance or object
System.out.println(new Solution1().k);
System.out.println("Ends"); // Prints 32:
// Here j is not local variable.
System.out.println(j); // Prints 22
}
}