Nice question. Isn't it? :)
I urge you to think about this for a moment before hurrying to the answer.
.
.
.
finalize() will run only once. Repeat with me, finalize() will run only once! So, the answer to above question is No.
My journey of facing the SCJP (Sun Certified Java Programmer) Exam.
![]() |
| SQL Joins Venn Diagram |
public class Parent{You can even call the main method of other class from one class directly as its a static method!!
public static void main(String args[]){
System.out.println("In Parents main method..");
}
class child extends Parent{
public static void main(String args[]){
System.out.println("In childs main method..");
}
Let me tell you some things in this example. The source file name is example6.java and there is no class called as example6 in the source file. There are two classes class1 and class2 which are not public!
Doubts Answered:
1. NO! I tried to give public access to class1 and class2 and compiler scolded me for two things - one he said that class1 if public should be declared in file named class1.java, other is - there can't be any two public classes in a single java file.
2. Not al all! You can see that class1 and class2 are not public, still I am calling both of them!
3. Yes it can be called if it has a main method! You can see that class1 is not public but has a main method, so I can call it by giving parameter class1 to the java command.
I hope you have throughly understood the constraints. Just to revise all, here is the conclusion...
Conclusion: The above program proves that...
1.There is no need that the classes whose main method you are going to call should be public.
2.File name can be anything as long as you dont have any public classes in your file.
3.Which main method will execute only depends upon which classname you pass to the JVM that is as argument to java command.
4. If you want global access of a class and hence declare that class as public then it should be in the same file as the class name, and as you cant have two different names for a single file you can't have two public classes in a single .java file!