+ 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

8th Jul 2018, 6:48 PM
Sukhbir Singh Khalsa
Sukhbir Singh  Khalsa - avatar
12 Respostas
+ 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.
8th Jul 2018, 7:37 PM
Eldar Bakerman
+ 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.
9th Jul 2018, 10:02 AM
Vivek Jadhav
Vivek Jadhav - avatar
+ 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
19th Aug 2018, 4:14 PM
reshma kachhadiya
reshma kachhadiya - avatar
+ 1
Is this a real question, or do you know the answer?
8th Jul 2018, 7:29 PM
Eldar Bakerman
+ 1
this is real question man!
8th Jul 2018, 7:30 PM
Sukhbir Singh Khalsa
Sukhbir Singh  Khalsa - avatar
8th Jul 2018, 7:30 PM
Sukhbir Singh Khalsa
Sukhbir Singh  Khalsa - avatar
+ 1
I would type more but I hate typing through phone 😝
8th Jul 2018, 7:37 PM
Eldar Bakerman
+ 1
ok Eldar Bakerman so There is only that reason for static not anything else , Dude ?
9th Jul 2018, 12:27 AM
Sukhbir Singh Khalsa
Sukhbir Singh  Khalsa - avatar
+ 1
Vivek Jadhav that's the purpose of public and static in general. Not why it's in this method.
9th Jul 2018, 10:23 AM
Eldar Bakerman
+ 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.
9th Jul 2018, 10:28 AM
Eldar Bakerman
+ 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 .
9th Jul 2018, 3:56 PM
Tarika
Tarika - avatar
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.
9th Jul 2018, 1:47 PM
Vivek Jadhav
Vivek Jadhav - avatar