Answer the question
In order to leave comments, you need to log in
How do functions work in JAVA?
Hello dear, below I will give the JavaScript code, please explain how to do the same in JAVA.
Answer the question
In order to leave comments, you need to log in
And if you really want to have a "global" text variable:
class Rextester {
public static String text;
public static void main(String[] args) {
text = "JavaRush. Learn once — use anywhere";
run();
}
public static void run() {
for (int i = 0; i < 10; i++) {
System.out.println(text);
}
}
}
The text variable is in the scope of the main method, the run method does not see it. The run method will start seeing String text if you move the String text into the scope of the class - as a class field. And since this field is accessed from a static method, this variable must be static, or access it by creating an instance of the Rextester class
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question