+ 2
Why void main in java is Public static ?
the most confusing question , most asked interview question , Let's see how much you know Java comment your answers
12 Réponses
+ 1
Well, let's start with the fact that every class by default inherits Object.
And Object (or some other interface that your class implements), needs to run your main method, it can access it only if its public.
Second of all, it is Static to avoid creating an instance of your main class.
+ 2
It's public and static becoz of following reasons
1. Any one can access the method.
2. Method can be invoked without instantiating the class.
+ 2
String is a pre-defined class name in java. ... The main method must be public so it can be found by the JVM when the class is loaded. Similarly, it must be static so that it can be called after loading the class, without having to create an instance of it. All methods must have a return type, which in this case isvoid.Apr 5, 2012
public st
+ 1
Is this a real question, or do you know the answer?
+ 1
this is real question man!
+ 1
I would type more but I hate typing through phone 😝
+ 1
ok Eldar Bakerman so There is only that reason for static not anything else , Dude ?
+ 1
Vivek Jadhav that's the purpose of public and static in general. Not why it's in this method.
+ 1
Sukhbir khalsa The class with the main method is called Driver, it drives all the process; This class can, for instance, represent a button that launches a game, that button is not for OOP purposes but only to run a line that starts a whole process, therefore creating instances of it is, logically, not a great idea and sometimes may even result in errors, therefore the main method is Static.
+ 1
as the program execution starts from main method , jvm will call main method first to execute program, but main method consists in a class and if you want to call method of any class you need object for that and objects are created runtime , but we can not call main method using object as main method class is not executed thats why we have to make main() static so that we can call by using class name .
0
Eldar Bakerman
Read my reply above. It's due to this reasons Main method is static and public. also The JVM executes main method only if it's static public , it gives JVM freehand for directly accessing main without reference and as it's public it can access, if not then program won't execute.