I have code like this:
public class Class1 {
public void method1() {
...
Class2 c = new Class2(i);
...
}
public Class1(int i) {
...
}
}
How can I get variable i from constructor to method1 ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to declare a field in the class. E.g.
private int i;Then in the constructor set
this.i = i;You can then accessifrom anywhere in the class.To be honest this is pretty basic stuff so Id suggest reading up on Java basics before continuing a project 🙂