Java public static void main Method
What is the main method in Java?
It is the entry point of any Java application. Syntax:
public static void main(String[] args)
Explanation of Each Keyword:
- public: JVM can access this method from anywhere.
- static: Method can be called without creating object.
- void: Method doesn't return anything.
- main: Standard method name where execution starts.
- String[] args: Takes input from command line as array of Strings.
Example:
public class Hello {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Frequently Asked Questions
- Why must the method be public? Because JVM must be able to access it from outside the class — otherwise it will give an error.
- Why is it static? If it were not static, we would need to create an object of the class. But JVM should run the method without needing that.
- What does void mean again? It means the method does not return anything. It just runs and ends.
- Can I rename args? Yes, you can change args to any other name:
public static void main(String[] myInput) { ... }
But don’t changemain
,String[]
, or the order — JVM won’t recognize it.
Summary Table:
Word | Type | Purpose |
---|---|---|
public | Access Modifier | Makes method accessible to JVM |
static | Keyword | No object creation needed |
void | Return Type | Nothing returned |
main | Method Name | Predefined name used by JVM |
String[] | Argument Type | Input arguments in form of String array |
args | Argument Name | Reference name for input data |
Java का main method क्या होता है?
यह किसी भी Java प्रोग्राम की शुरुआत करने का पॉइंट होता है। Syntax:
public static void main(String[] args)
हर शब्द का मतलब:
- public: यह method कहीं से भी accessible है
- static: object बनाए बिना ही method कॉल किया जा सकता है
- void: यह कोई value return नहीं करता
- main: Java execution यहीं से शुरू होता है
- String[] args: command-line से input के लिए
उदाहरण:
public class Hello {
public static void main(String[] args) {
System.out.println("Java में आपका स्वागत है!");
}
}
अक्सर पूछे जाने वाले सवाल
- method public क्यों होना चाहिए? क्योंकि JVM इसे बाहर से access करता है।
- यह static क्यों है? ताकि इसे object बनाए बिना call किया जा सके।
- void का क्या मतलब है? इसका मतलब method कोई value return नहीं करता।
- args का नाम बदल सकते हैं? हाँ, इसे कुछ भी रख सकते हैं जैसे:
public static void main(String[] myInput) { ... }
लेकिनmain
,String[]
या उनकी order को न बदलें।
सारांश तालिका:
शब्द | प्रकार | उद्देश्य |
---|---|---|
public | Access Modifier | JVM कहीं से भी access कर सके |
static | कीवर्ड | object की जरूरत नहीं |
void | Return Type | कुछ return नहीं करता |
main | method नाम | execution की शुरुआत |
String[] | argument प्रकार | command-line से इनपुट |
args | argument नाम | इनपुट का reference |