K
K
Kirill Bazhenov2017-09-29 20:56:23
Java
Kirill Bazhenov, 2017-09-29 20:56:23

How can I use one variable in different methods and classes?

public class Test {
    public static void main(String[] args) {
        int a = 10;
    }

    public static void G() {
        System.out.println(a);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-09-29
@Pain123

Declare it as a class field.

public class Test {
    private static int a;

    public static void main(String[] args) {
        int a = 10;
    }

    public static void G() {
        System.out.println(a);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question