问题
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