How to use static variable in Java

1 Answer

0 votes
public class MyClass {
    static int counter = 0;
    public static void f() {
        counter++;
        System.out.println(counter);
    }

    public static void main(String args[]) {
       f();
       f();
       f();
    }
}



/*
run:

1
2
3

*/

 



answered Apr 5, 2019 by avibootz

Related questions

2 answers 356 views
1 answer 269 views
1 answer 316 views
1 answer 245 views
245 views asked Jan 22, 2025 by avibootz
2 answers 216 views
2 answers 203 views
203 views asked Jan 22, 2025 by avibootz
...