Sol: It’s a contract between java and programmer.
Main syntaxes:
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.
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.
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.
Sol: If main method is static then it’s indicating that this method can be called without creating an object of a class.
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.
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.
Sol: When its task completed. main thread task is to run main method and when each of line executed the thread is over.
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.
Sol: Yes we can but we can’t override it.
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.
Sol: Yes.
Sol: Yes.
Save It as A.java
Then it will compile: javac A.java
Run
:java A
:java B
Sol: Save It as A.java
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.
Sol: Yes but can’t run.
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.
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.