A java program can contain any number of classes but at most one can be declared as public. If there is a public class then name of the program and name of the class must be matched.
otherwise we will get compile time error.
Example:
class A
{
}
class B
{
}
class C
{
}
Case 1: If there is no public class then we can use any name and there are no restrictions. Like A.java, B.java,C.java and John.java etc.
Case 2: If class B is public then name of the program should be B.java otherwise we will get compile time error saying Class B is public, should be declared in a file named B.java
Case 3: If class B and C declared as public and the name of the program is B.java then we will get compile time error saying class C is public, should be declared in a file named C.java
class A
{
public static void main(String args[])
{
System.out.println("A class main");
}
}
class B
{
public static void main(String args[])
{
System.out.println("B class main");
}
}
class C{
public static void main(String args[])
{
System.out.println("C class main");
}
}
class D{
}
Save above program with Student.java
Now when we compile it with Student.java then A.class,B.class,C.java and D.java file will be generated.
Now run the program with different class name.
java A
Output: A class main
java B
Output: B class main
java C
Output: C class main
java D
Output: found exception like NoSuchMethodError: Main
java Student
Output: Found error like NoClassDefFoundError: Student
Conclusion:
1. Whenever we are compiling a java program for every class present in that program, a seprated class file is genrated.
2. We can compile a java program (source file) but we can run a java class.
3. Whenever we are executing a java class the corresponding class main method will be executed. If the class does not main method then we will get run time exception saying NoSuchMethodError: main
4. If the corresponding .class file not available then we will get run time exception saying NoClassDefFoundError.
5. It is not recommended to declared multiple classes in a single source file. It is highly recommended to declare only one class per source file and the name of the
program we have to keep same as class name. The main advantage of the approach is readability and maintainability of the code will be improve.
Import Statement:-
class Test{
public static void main(String args[]){
ArrayList l= new ArrayList();
}
}
After run this above code find error with "cannot find symbol"
symbol: class ArrayList
Location: class Test
We can solve this problem by using fully qualified name like
class{
public static void main(String args[]){
java.util.ArrayList l = new java.util.ArrayList();
}
The problem with uses fully qualified name every time is, it increases length of the code and reduces readability.
We can solve this problem using import statement.
Whenever we are writing import statement it is not required to use fully qualified name every time. We can use short name directly.
import java.util.ArrayList;
class Test{
public static void main(String args[]){
ArrayList l= new ArrayList();
}
}
No comments:
Post a Comment