I have two question in my mind.
can we create java file having two classes and both have main methods?

is it possible or not? why?

if possible then we have to save it with which class name?

and one more question is:

BufferedReader br = new InputStreamReader(new FileInputStream(new File("abc.txt")));
or
FileReader ff = new FileReader("abc.txt");

Which one is good to use at which situation?

Thanks

question 1: sure ... why not?
you can have an unlimited nr of classes in one .java file. and each class can have an unlimited amount of main methods (overloading). if you are talking about a main method with the signature of thé main method, then, no. only a top-level type, or a static type can contain static methods.

so:

public class ClassA{
  public static void main(String[] args){}  
  private class ClassB{
    public static void main(String[] args){

    }
  }
}

this won't compile, since ClassB is neither a top-level type (like ClassA), nor a static type.

your Java file has to have the exact same name as the public class defined in it. you may have an unlimited amount of classes inside one file, but only one public class.

so, in the above example, the filename would be ClassA.java

about your second question, there are tons of comparisons that can be found online about that, this one, for instance.

commented: really good answer +4

There's more to this than it seems. Here's a simple single source file

public class MulipleMains {
   public static void main(String[] args) {
      System.out.println("One");
   }
}

class MulipleMains2 { // can't be public
   public static void main(String[] args) {
      System.out.println("Two");
   }
}

Eclipse will let me run that single file from either main method.
I also created two runnable jars with the same .class file, but different manifests, and can thus run wither main method.

apparently I'm just a tad to late to edit my previous post, so I'll add it in a new one:

public class Test {
    public static void main(String[] args) {
    System.out.println("nope");
    String[] arr = { "yes", "this", "works" };
    Mock.main(arr);
    }

    private static class Mock {
    public static void main(String[] args) {
        System.out.println("Mock");
        for (String el : args) {
        System.out.print(el + " ");
        }
    }
    }
}

The above code clearly has two classes within one file. and they both have a main method which can be used as an entry point for an application, off course, the output will depend on which main method you call.

the reason why this will work, is because here, Mock is a static type, so even while it's not a top-level type, it still can contain static methods.

I just tried that version (with the second class as a static inner class) and I can't run the inner main
"Error: Could not find or load main class MulipleMains2"

can we create java file having two classes and both have main methods?

Based on some trials, and a quick scan through the JLS, I believe this is the answer:

Yes, and you can use either as the entry point, subject to...
Both classes must be top-level classes (not inner classes).
Only one can be public, and the .java file must have the same name as the public class.
There is no equivalent question for .class files because a .class file only contains one class.

EDIT:
It turns out you can use a main method from an inner (static) class as well. You just need to use the fully-qualified class name, eg
Main-Class: MulipleMains$MulipleMains2

I tried to run that through my eclipse front here in a "dead moment", and that worked, so I assume going through the command prompt it should be possible as well.

EDIT: didn't read your last post completely before answering on your previous one, but yup, that's it :)

Thanks for your reply.

@stultuske:
that means we can create one class inside another class and both should have main method.but one class must be public and other is private or not public.

So how we about to know that which class's main method is called?
how to call main method of inside class?

no, that means both COULD have main methods. a class is never (EVER) forced to have a main method.
the basics of it is: you can write an unlimited amount of classes in one file, they might be one parent and several classes created within that parent, or, as James pointed out, just two (what might seem) parent classes (which can, of course, contain inner classes).

but, and as always, there's a but:
there is never more then one public class allowed in a .java file. this class must have the exact same name (case sensitive) as the .java file it's in, so: public class ClassA{ should be placed in a file called ClassA.java

commented: good to know... +0

So how we about to know that which class's main method is called?

When you run the program (with java.exe or javaw.exe), either (1) you specify a .class file or (2) you specify a .jar file containing a manifest file that names a class. Either way you specify the class.

how to call main method of inside class?

You just need to know the name of the .class file that contains the compiled inner class. It will look like
Outerclass$InnerClass. (This would be an unusual thing to do.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.