HTML Java

Java Main Method Interview Questions


Ques: Why public void main method(String args[]){} is like this?

Sol: It’s a contract between java and programmer.

Main syntaxes:

  • public static void main(String args[])
  • static public void main(String []args)
  • final static public void main(String …args)
  • synchronize, strictfp, final static public void main(String …args)

Ques: Is main is user define or predefine?

Sol: Neither predefine nor userdefine.

We can say userdefine because we define it in our own program. And it’s a contract also in java that to execute your java program that main must be in your program.


Ques: Why main method is public?

Sol: If any method is not public in java then it will be default method.so it can’t call outside the package.

So that’s why it’s public and accessible anywhere outside the Program in different package.


Ques: Who is Responsible to call main method?

Sol: JVM (it’s a software program) suppose, if jvm is in another package and my program is in another package and main method is not public then JVM can’t call main method outside the package.


Ques: Why its static?

Sol: If main method is static then it’s indicating that this method can be called without creating an object of a class.

  • Because static method can be called with its class name so that When JVM calls main method it no needs to create an object.
  • If it creates an object then it takes more time to create an object after that store it so its memory wastage.
  • So to stop wasting memory and extra effort to create an object method is static.

Ques: Why return type is void not int or else?

Sol: Because main method calls by jvm and if we return anything then what it does further so that’s why return type is void.

If someone said I want to write int instead of void so he couldn’t write so because it’s a contract in java. You can but it becomes user define method.


Ques: How java program run?

Sol: Name of java tool with class name

Syntax: Java test

And java tool start jvm. jvm is a software program when program executes then process creating in memory on process main thread and gc threads are running and all programs executes on main thread.

Java program is not on control of OS and its run over the OS in separate thread. Here is the diagram.

Thread is a subprocess inside process.


Ques: When thread is dead?

Sol: When its task completed. main thread task is to run main method and when each of line executed the thread is over.

Main:
  • thread
  • method
  • group

Ques: What is main?

Sol: It’s just name or identifier and it is an entry point of the program.

Someone think that I want to create same name as main method is so it’s not possible that programs have same name method if still he want then he needs to overload that method just change the argument type.


Ques: Can We overload main method?

Sol: Yes we can but we can’t override it.


Ques: Why main method argument is string?

Sol: So that we can pass values from command line argument its used to pass values at runtime.

Save It as A.java

class A {
public static void main(String args[]){
System.out.println(“Hello A”);
}
}

class B {
public static void main(String args[]){
System.out.println(“Hello B”);
}
}

Then it will compile : javac A.java

Run : java A
: java B

Both will run but writing 2 mains in a program its not a good practice. Because entry points will be separate for every class so we must create 1 entry point in a program.


Ques: Can we create a main method inside every class in a program?

Sol: Yes.


Ques: Can you call the main method explicitly in the program?

Sol: Yes.

Save It as A.java

Then it will compile: javac A.java

Run
:java A
:java B


Ques: How to Overload a main method?

Sol: Save It as A.java


Ques: How many classes can a single java file?

Sol: Many class but public only 1 class and file name should be same of public class. To compile javac filename.java and byte code of every class stored in the separate .class file.


Ques: Can we compile without main?

Sol: Yes but can’t run.


Ques: What is implicit compilation in Java?

Sol: If you have 2 files

Save it as A.java
class A{
}

save it as B.java
class B{
public static void main(String args[]){
A a=new A();
}
}

First we compile and run B then what do you think what happen?

First A will compile before B compilation it’s called implicit compilation for this class name and file name must be same if it’s not then implicit compilation will not work.


Ques: Why file name and class name must be same?

Sol: Because suppose you have public class A in x file and public class A in y file so when we run B.java above program then compiler get confused that from where we have to bring class A either from x file or y file that why in case of public class the file name must be same.