Monday, September 17, 2012

Java Interview Questions - Class/Object


Can we create an Empty java file?

Yes, it is valid to create an empty Java file. The compilation will be successful but no class file will be created. It’s just that the compiler won’t throw any error.
As there is no class file gets generated, we can’t even try running the empty file.


Can we create an Empty class?

Yes, we can create a class which don’t have any variables or methods. Once the file is compiled, we get a class file.


What happens when we run a java class without main method? 
We get a classNotFoundException.The JVM when tries to run an file, tries to find the public class present in the file and then tries to search for a valid main method in that particular class.
As, in this case, we don’t have any main method, JVM complains that it didn’t find any public class which has a valid main method.
The below will be the exception stack trace
Exception in thread "main" java.lang.NoClassDefFoundError:
Caused by: java.lang.ClassNotFoundException:
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class:.  Program will exit.



Can a file contain more than one class?
Yes, but only one public class is allowed in a file.
When a file is executed using the java command, it searches for a main method in the public class to start its execution. So, if the class have two public class, it won’t be able to figure out in which class it needs to search for the main method.
But it is perfectly possible to have more than one class in a file until that particular file has only one public class.
Screenshot showing two classes in a file:






Once this file is compiled, two class files will be generated, one for each

Compiler giving error when having more than one public class in a file:








How many class files are generated when you compile a java file having two classes?

Two class files. One for each class is generated.

Can the main method have any return type other than void?
The JVM searches for the public class in the file specified to run and then tries to find the main method which is static having String[] as arguments and whose return type is void.
If the return type is changed from void, an exception will be throws saying Main method is not found.
For example, compiling and running the below file:






Will result in the below exception when we try to execute.
Exception in thread "main" java.lang.NoSuchMethodError: main
But when another main method is also present which is the original one, then the compiler shows an error saying duplicate method.







Can we change the argument list of the main method?
Yes, but JVM won’t treat it as an original main method. See the above question for explanation.



Can we overload the main method?
Once the file is run using the java command, it goes to the public class in that file and searches for the main method having void as return type and which is static and which takes String[] as arguments. So, it does not bother to have another main method with different signature. The JVM calls only the original main method.
The other main method should be called manually in the code, the same way it applies for other normal methods. 






Can we have multiple methods with the name “main” in a single class?

Yes, But the method which is static and have the return type as void and having Sting[] as arguments only will be called by the JVM.
For example, the below is a valid class file which don’t give any compilation or Run time error.


The above program on execution, prints the below output:
From the Original main method




Can we have two main methods in our application in two different classes?
Yes, any application can have multiple classes having the main method. It is up to the user to choose which one he wants to execute.



What is a class?
A class is a blue print to create objects.

What is an object?
Object is an instance of the class.