Error: Main method not found in class myPackage.inheritance

若如初见. 提交于 2019-12-30 14:38:13

问题


package myPackage;

public class inheritance {
     int salary = 50000;
}

class worker extends inheritance {
    int bonus = 10000;

    public static void main(String[] args) {
        worker obj1 = new worker();
        System.out.println("employee salary is" + obj1.salary);
        System.out.println("employee bonus is" + obj1.bonus);
    }
}

Hi.. I am new to java. I am trying to write an inheritance program and getting this error.

Error: Main method not found in class myPackage.inheritance, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application


回答1:


Try to move the main method inside of inheritance class like this:

public class inheritance {
    int salary = 50000;
    public static void main(String[] args) {
        worker obj1 = new worker();
        System.out.println("employee salary is" + obj1.salary);
        System.out.println("employee bonus is" + obj1.bonus);
    }
}

class worker extends inheritance {
    int bonus = 10000;
}



回答2:


This error can happen in multiple ways

First need to clarify how you are compiling and running your program

1.Ensure that the java files are placed in the correct package(folder) itself

2.Ensure location of your class file is added to your class path variable

Most probably your problem will be solved by making the inheritance class as public.

package myPackage;
class inheritance {
int salary = 50000;}
public  class worker extends inheritance {
int bonus = 10000;
public static void main(String[] args) {
worker obj1 = new worker();
System.out.println("employee salary is" + obj1.salary);
System.out.println("employee bonus is" + obj1.bonus);
}
}


来源:https://stackoverflow.com/questions/34195383/error-main-method-not-found-in-class-mypackage-inheritance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!