Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Your question is a bit odd, but it's understandable. Let's not 1/2.java (because class names must match file names, and in java classes cannot be named numbers), but at least First.java and Second.java
In Second.java, write first
import First;
and further on the code in Second.java write
x=First.var1
This is if var1 is static. Otherwise, you would first have to instantiate the First object and then call access to var1 on behalf of the instance.
Although personally it seems to me that you have just started learning java. See section Scopes, Modularity, your question is from there.
You can pass it to the constructor when creating an instance of class 2
public class Main {
static String x = "Тут ваша строка";
public static void main(String[] args) {
Main2 Main2 = new Main2(x);
}
}
public class Main2 {
String x;
public Main2(String x) {
this.x = x;
Main.x = x; // Это второй вариант но только если переменная static
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question