-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariables.java
More file actions
36 lines (29 loc) · 1.16 KB
/
Variables.java
File metadata and controls
36 lines (29 loc) · 1.16 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
package JavaBasic;
public class Variables {
static int z = 12; // Static variable
public int h = 12; // Instance variable
public static void main(String[] args) {
System.out.println("variables in java");
// Static variable
//it can be accessed anywhere in the program with out creating the instance
// of the class and a change at one place in the varible is reflected in every part
// of the code where the variable is used
System.out.println("Static variable z: " + z);
// Call a static method
sum(z);
// Create an instance to access the instance variable
Variables obj = new Variables();
System.out.println("Instance variable h: " + obj.h);
// Local variable demonstration
int a = 10; // Local variable
if (a > 5) {
int b = 5; // Local variable inside block
System.out.println("Local variable b: " + b);
a += b;
}
System.out.println("Local variable a after modification: " + a);
}
public static void sum(int num) {
System.out.println("Sum of num and z: " + (num + z));
}
}