调用静态方法
为什么这段代码显示编译错误-void is a invalid type for the variable test
public class Tester{
public static void main(String[] args){
static void test(String str){
if (str == null | str.length() == 0) {
System.out.println("String is empty");
}
else {
System.out.println("String is not empty");
}}
test(null);
How come this piece of code shows compiling error-void is an invalid type for the variable test
public class Tester{
public static void main(String[] args){
static void test(String str){
if (str == null | str.length() == 0) {
System.out.println("String is empty");
}
else {
System.out.println("String is not empty");
}}
test(null);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您尝试在另一个方法 (
main
) 中声明一个方法 (test
)。不要那样做。将test()
直接移到类中:(请注意,我还修复了缩进和空格。您可以使用各种约定,但您应该保持一致,并且比您问题中的代码已显示。)
You're trying to declare one method (
test
) within another method (main
). Don't do that. Movetest()
to be in the class directly:(Note that I've also fixed up the indentation and whitespace. There are various conventions you can use, but you should be consistent and rather clearer than the code in your question showed.)
您不能在另一个方法中声明一个方法。从
main
中推迟test
。而且您也无法从类中调用方法。test(null);
必须位于方法内部,例如 main。You can not declare a method inside another method. Put off
test
frommain
. And you also can't call a method from the class.test(null);
must be inside a method, say main.您的方法测试位于另一个方法内。
只需将测试方法放在 main 之外即可。
Your method test is inside another method.
Just put test method outside of your main.
您还必须添加双精度或操作数 (||) 才能正常工作而不会出现空指针异常错误。
You also have to add the double or operand (||) in order to work properly without Null Pointer Exception Error.
您在 main 方法中声明测试方法。因此,将测试方法与 main 方法分开。
You are declaring test method inside main method. So, separate the test method from main.