kavie
kavie

Reputation: 2244

Error: please define the main method as: public static void main(String[] args)

While Executing below program I'm getting error message as "Error: Main method not found in class Parent_Package.Parent, please define the main method as: public static void main(String[] args)".................. Can any one know how to resolve this??

First Package
==============
package Parent_Package;

public class Parent {
     int money = 100;enter code here
     protected  void give_Money(){
    money = money-10;
     System.out.println(money);
}
}

======================
Second Package
===================================
 package Child_Package;

 import Parent_Package.Parent;

 class Child extends Parent{
        public void donate() {
            give_Money();
        }

    }

 class Friend {
     public static void main(String[] args){
    Child c = new Child();
    c.donate();

    }
}

==================================================</body></html>

Upvotes: 0

Views: 9678

Answers (4)

tryingToLearn
tryingToLearn

Reputation: 11663

Since you haven't marked the Parent class as public, the "default" access modifier will be assigned. In java the classes can only see the "public" classes present in another package. Since your Friend is marked default (by compiler), Child class won't be able to see the Parent class.

Upvotes: 0

javadev
javadev

Reputation: 1669

By default, running a file with several classes will run the public one (only one can be public in a single file). In your code Parent is the public calss which does not contain a main method. That's why it doesn't find a main method.

Upvotes: 0

Azodious
Azodious

Reputation: 13882

The class which has main method should be marked as public.

So, make your class Friend as public class Friend

and

run java Friend instead of java Parent

Upvotes: 2

cowls
cowls

Reputation: 24354

When running this Java program you need to run the Friend class as this is the only one with a main method.

It looks like you are running the Parent class which does not have a main method defined.

Upvotes: 8

Related Questions