V
V
Volodar Znatsky2016-06-09 14:25:09
Java
Volodar Znatsky, 2016-06-09 14:25:09

How to pass a variable from class to class?

I need to transfer a variable from the main class to another class. Process it there and return the result back. They wrote that it was necessary to pass variables by creating objects, but in this way something did not work out for me. Explanatory search engines did not give me any more.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ItsGreyDay, 2016-06-09
@ItsGreyDay

public class A {
    private int privateA;
    public int publicA;

    public int getPrivateA() {
        return privateA;
    }

    public void setPrivateA(int privateA) {
        this.privateA = privateA;
    }
}

public class Main {

    public static void main(String[] args) {
       A a = new A();
        a.setPrivateA(777);
        a.publicA = 888;
        System.out.println(a.getPrivateA());
        System.out.println(a.publicA);
        int temp_value = a.getPrivateA();
        temp_value = temp_value * 25;
        a.setPrivateA(temp_value);
        System.out.println(a.getPrivateA());
    }
}

Conclusion:
777
888
19425

It is recommended to work with variables as a privateA variable, not as a publicA variable.

S
Sanan Yuzb, 2016-06-09
@Sanan07

Create getters and setters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question