Skip to content

Commit 80f94e2

Browse files
committed
feat: Demonstrate behavior of static and non-static variables across method calls
🧠 Logic: - `static int a = 10;` is shared across all instances of the class. - `int b = 10;` is re-initialized every time `fun()` is called. - On each call to `fun()`: - `a` retains and increments its value. - `b` resets to 10, increments to 11, but the new value doesn't persist. Signed-off-by: Somesh diwan <[email protected]>
1 parent 2b8ca1b commit 80f94e2

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
public class staticvariable {
22
static int a=10;
3-
void fun()
4-
{
3+
4+
void fun() {
55
int b=10;
66
System.out.println(a+" "+b);
77
++a; ++b;
88
}
9-
public static void main(String[] args)
10-
{
9+
public static void main(String[] args) {
1110
staticvariable s = new staticvariable();
1211
s.fun();
1312
s.fun();
1413
}
15-
}
14+
}

0 commit comments

Comments
 (0)