变量在引用时找不到Main Driver类

发布于 2024-12-11 23:19:15 字数 647 浏览 0 评论 0原文

我现在遇到静态和非静态错误的问题。有某些变量无法找到 main 方法。

我通过创建一个实例化该类的单独文件来构建该程序。像这样:

public class StartUp {
    public void main(String[] args) {
        MainDriver theMainDriver = new MainDriver();
        theMainDriver.start();
    }
}

在程序的某些类中,它将变量传递回 mainDriver。但是当我尝试引用它时,我收到错误“找不到符号变量theMainDriver”。

例如:

 public void getEmployee()  {
      theMainDriver.setEmployee(theEmployee);
 }

 public void getEmployeeID() {
     theMainDriver.setEmployeeID( randomIDno);
 }

我如何以使其对其他类更可见的方式声明主驱动程序。 如果我这样做 MainDriver.setEmployeeID( randomIDno); 它确实找到了主驱动程序但它存在无法从静态上下文引用非静态方法的问题。

Im having issues now with static and non staic errors. with sertain variables unable to find the main method.

I have strated the program by creating a seperate file which instantiates the class. like so:

public class StartUp {
    public void main(String[] args) {
        MainDriver theMainDriver = new MainDriver();
        theMainDriver.start();
    }
}

Within certain classes in the program it passes variables back to the mainDriver. But when I try to refrence it to, I get the error "cannot find symbol variable theMainDriver" .

e.g:

 public void getEmployee()  {
      theMainDriver.setEmployee(theEmployee);
 }

 public void getEmployeeID() {
     theMainDriver.setEmployeeID( randomIDno);
 }

how can I declare the main Driver in a way that makes it more visible to other classes.
It does find the main driver if I do this MainDriver.setEmployeeID( randomIDno); but then it has issues with non static method cannot be referenced from a static context.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-12-18 23:19:16

您可以执行以下操作:

public class StartUp {

    public static MainDriver theMainDriver;

    public void main(String[] args) {
        theMainDriver = new MainDriver();
        theMainDriver.start();
    }
}

并从任何类调用,如下所示:

Startup.theMainDriver.setEmployeeID(randomIDno);

You can do the following:

public class StartUp {

    public static MainDriver theMainDriver;

    public void main(String[] args) {
        theMainDriver = new MainDriver();
        theMainDriver.start();
    }
}

And call from any class as follows:

Startup.theMainDriver.setEmployeeID(randomIDno);
清风无影 2024-12-18 23:19:16

如果出现编译错误,则有 3 件事。

1) MainDriver 是公开的吗?
2) 如果MainDriver在不同的包中,你导入它了吗?
3)如果MainDriver来自不同的包或不同的项目或外部jar,你是否在类路径中给出了它?

另外,正如其他人所说,您获得了 main(String[] args)static 部分

3 things if there was a compilation error.

1) Is MainDriver Public?
2) if MainDriver is in different package, did u import it ?
3) If MainDriver is from diffrent pacakage or different project or a external jar, Did u give that in the classpath ?

also, as the othe guy said, u for got the static part of the main(String[] args)

猫九 2024-12-18 23:19:16

首先, main 的声明应该就像

public static void main(String[] args)

我无法理解你如何在不使其静态的情况下运行程序

其次,你没有给出 MainDriver 类的结构。可能不是公开的。因此,请将您的 MainDriver 类公开。我认为这会解决问题

Firstly, the declaration of main should be like

public static void main(String[] args)

I cant understand how you are running the programm without making it static

Secondly, You didn.t gave the structure of your MainDriver class. May be it is not public. So make your MainDriver class public. I think that will resolve the problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文