M
M
Max2016-11-27 19:03:27
privacy
Max, 2016-11-27 19:03:27

How to pass one variable from one class to another?

Hello! In general, I encountered such a problem: there is a class Test, in which there is a variable s, which is set in the constructor of this class. There is also a class inerClass, to which this variable is also passed through the constructor.
Further, all this business is transferred to inStr through the print () method. For inStr, a getter is created with the help of which the whole thing is displayed on the screen. At the output I get null instead of "zzz". I would like to understand why. Thank you in advance.
public class Test {
String s;
inerClass inerClass=new inerClass(s);
String inStr=inerClass.print();
public Test(String s)
{
this.s=s;
}
public String getInStr() {
return inStr;
}
public class inerClass
{
String inStr;
public inerClass(String w) {
this.inStr=w;
}
public String print(){
return inStr;
}
}
public static void main(String[] args) {
Test test= new Test("zzz");
System.out.println(test.getInStr());
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2016-11-27
Quite @queetman

Understood. You need to initialize innerClass in the constructor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question