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 236 views
1 answer 170 views
1 answer 216 views
1 answer 154 views
154 views asked Jan 22, 2025 by avibootz
2 answers 111 views
2 answers 97 views
97 views asked Jan 22, 2025 by avibootz
...