【问题标题】:Static method not being called Java未调用 Java 的静态方法
【发布时间】:2020-03-29 22:41:14
【问题描述】:

由于main方法是静态的,所以应该可以静态调用。因此,我使用:

package prototype.server.main;

import javax.swing.JFrame;
import prototype.server.main.gui.Swing;

public class Runtime {
    public static void main(String[] argv) {
            Swing swing = new Swing(true, argv[0]);

        @SuppressWarnings("unused")
            JFrame maingui = swing.getGuiFrame();
    }
}

作为静态主代码,然后调用使用:

import prototype.server.main.Runtime;

public class Main {
     Runtime.main(new String{"f"});
}

调用静态方法,但 Eclipse 给我一个错误。请帮助并提前感谢您。

【问题讨论】:

  • 它给你的错误是......?因为现在您正在调用一个带有“不是字符串数组”的字符串数组的方法,所以出现错误很有意义。另外,当"f" 已经是那个东西时,为什么还要使用new String{"f"}
  • 什么错误
  • 您的方法调用既不在方法内部,也不在静态初始化块内部。这就是你需要解决的问题。
  • 什么是new String{"f"}?也许你的意思是new String[]{"f"}
  • 要更新/澄清您的问题,请使用edit 选项。

标签: java static-methods


【解决方案1】:

我们需要修复两个错误;

    import prototype.server.main.Runtime;

public class Main {
// add constructor, or method that you can call another method
// or make this static { ... } block that fits you
public Main() {
//do not forget [] for array
     Runtime.main(new String[]{"f"});
}
}

【讨论】:

  • 我只需要主要方法,但构造函数足够接近,感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 2020-01-21
  • 1970-01-01
  • 2011-01-03
相关资源
最近更新 更多