Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Is main method required to be written inside a certain class?

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is main method required to be written inside a certain class?

    I include the main method in one of my class, but the following error still appeared.
    "Error: Main method not found in class Maximizer, ... "

    However, if I moved the main method to class Maximizer, the problem solved.

    Is main method required to be written inside a certain class? If so, what's the rule?
    Or are there other solutions to this problem?

    Thanks.

    -------------------------my codes-------------------------


    public class DogLauncher {
    public static void main(String[] args){
    Dog d1 = new Dog("Elyse", 3);
    Dog d2 = new Dog("Sture", 9);
    Dog d3 = new Dog("Benjamin", 15);
    Dog[] dogs = new Dog[]{d1, d2, d3};
    System.out.println(Maximizer.max(dogs));
    Dog d = (Dog) Maximizer.max(dogs);
    d.bark();
    }
    }


    public class Maximizer {
    public static Comparable max(Comparable[] items) {
    int maxDex = 0;
    for (int i = 0; i < items.length; i++) {
    int cmp = items[i].compareTo(items[maxDex]);
    if (cmp > 0) {
    maxDex = i;
    }
    }
    return items[maxDex];
    }
    }

    public interface OurComparable {
    /* Return negative number if this < o.
    * Return 0 if this equals o.
    * Return positive number if this > o.
    */
    public int compareTo(Object o);
    }

    public class Dog implements Comparable<Dog>{
    private String name;
    private int size;

    public Dog(String n, int s){
    name = n;
    size = s;
    }

    public void bark(){
    System.out.println(name + " says: bark");
    }

    /* Returns negative number if this dog is less than the dog pointed at by o. */
    public int compareTo(Dog uddaDog){
    return this.size - uddaDog.size;
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Is main method required to be written inside a certain class?

    Is main method required to be written inside a certain class? If so, what's the rule?
    The main method is called by the java program to start execution of the class that is passed to it on the command line.
    If you enter: java TheClass
    then TheClass needs to have a main method.

    When posting code please wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to the main method for this class
    By TheVolum1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2014, 07:47 PM
  2. Splitting up main method / class
    By Scren in forum What's Wrong With My Code?
    Replies: 53
    Last Post: February 8th, 2014, 04:54 PM
  3. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  5. [SOLVED] method inside main()
    By rohan22 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2011, 11:42 PM