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 239 views
1 answer 173 views
1 answer 219 views
1 answer 158 views
158 views asked Jan 22, 2025 by avibootz
2 answers 113 views
2 answers 103 views
103 views asked Jan 22, 2025 by avibootz
...