-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwelfth.java
More file actions
23 lines (21 loc) · 871 Bytes
/
twelfth.java
File metadata and controls
23 lines (21 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Write a program to show the scope and lifetime of a variable.
public class twelth {
static int globalVar = 100;
public static void main(String[] args) {
System.out.println("Inside main method");
int localVar = 200;
System.out.println("Global variable: " + globalVar);
System.out.println("Local variable in main: " + localVar);
{
int blockVar = 300;
System.out.println("Block-level variable: " + blockVar);
}
showScope();
}
static void showScope() {
int localToShowScope = 400;
System.out.println("Inside showScope method");
System.out.println("Global variable (accessible): " + globalVar);
System.out.println("Local to showScope(): " + localToShowScope);
}
}