英语警报不好每当我尝试在 main 之外的另一个类上使用 System.out.println 时,安装在我 PC 中的每个 IDE 都会在标题上返回错误。
我正在编写一个非常简单的代码。
在IntelliJ上,我已经尝试使用"无效缓存",但效果不佳。
在这里工作:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
System.out.println("Hi"); /* <-- This works on main, but doesn't
work in any other class opened in
another tab*/
}
但不是在另一个选项卡中:
package javaapplication3;
public class NewClass {
System.out.println("Hi");
}
更新
这里有一些图像来指定问题:在这里工作,但不是在这里。
所有代码都必须在方法中。
命令System.out.println("")
仅在方法中起作用。
将其放在类而不是方法下将导致编译器引发错误。
例如:
public class test { // class
public static void main(String args[]) {
// inside main method
System.out.println("Hello, World!"); // correct
}
}
将正常工作。
但是,如果您将命令放在一个类下,则会导致错误。[需要一定的方法]
例如:
public class test{
// inside a class, but no method
System.out.println("Hello, World!"); //incorrect
}
此外,您需要确保您的类和函数不是保留关键字。
从您上面指定的内容来看,main
不是类,而是类的主要方法。