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 362 views
1 answer 274 views
1 answer 319 views
1 answer 247 views
247 views asked Jan 22, 2025 by avibootz
2 answers 217 views
2 answers 205 views
205 views asked Jan 22, 2025 by avibootz
...