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 245 views
1 answer 179 views
1 answer 232 views
1 answer 166 views
166 views asked Jan 22, 2025 by avibootz
2 answers 118 views
2 answers 107 views
107 views asked Jan 22, 2025 by avibootz
...