Answer 1: Importing package has nothing to do with how many main
methods you have. main
method under public class will be executed by default.
Answer 2: Your java file must have same name as your public class. See this stackoverflow answer to the question Can a java file have more than one class?:
Yes, it can. However, there can only be one public top-level class per
.java file, and public top-level classes must have the same name as
the source file.
The purpose of including multiple classes in one source file is to
bundle related support functionality (internal data structures,
support classes, etc) together with the main public class. Note that
it is always OK not to do this--the only effect is on the readability
(or not) of your code.
Basically you can declare public static void main(String[] args)
inside that public class. Which you did in class OOP3
.
Answer 3: If you have different classes in a package you can use them like this:
import packagename.ClassA;
import packagename.ClassB;
public class OOP {
public static void main(String[] args) {
ClassA objA = new ClassA();
ClassB objB = new ClassB();
objA.doSomething();
// etc.
}
}