Java Methods परिचय
In this page:
modifier returnType methodName(parameters) {
// body
}
methodName(arguments);
Creating and Calling a Method
एक method statements की एक sequence को एक नाम के तहत bundle करता है, और वह code असल में सिर्फ तब चलता है जब कुछ explicitly उस method को नाम से call करे -- method लिखना इसे execute नहीं करता।
उदाहरण: Creating and Calling a Method
// Define the class Main
public class Main {
static void greet() {
// Print a line to the console
System.out.println("Hello!");
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
greet();
}
}
Login to try C/C++/Java/PHP code in the editor
Method Declaration Structure
किसी method की declaration पहले से चार चीज़ें specify करती है: इसे कौन call कर सकता है (access modifier), यह किस type की value वापस देता है (return type), इसका नाम, और यह कौन सा data accept करता है (parentheses में parameters)।
उदाहरण: Method Declaration Structure
// Define the class Main
public class Main {
public static int square(int number) {
// Send number * number back to the caller
return number * number;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(square(4));
}
}
Login to try C/C++/Java/PHP code in the editor
Flow of Execution
Execution हमेशा main में शुरू होता है; जब भी code किसी दूसरे method की call पर पहुंचता है, control उस method की body में jump करता है, इसे completion तक चलाता है, और फिर original call के बिल्कुल बाद वाली line पर return होता है।
उदाहरण: Flow of Execution
// Define the class Main
public class Main {
static void step2() {
// Print a line to the console
System.out.println("Inside step2");
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Before call");
step2();
System.out.println("After call");
}
}
Login to try C/C++/Java/PHP code in the editor
Variable Scope
किसी method के अंदर declared एक variable सिर्फ उस method के execution के लिए मौजूद है और हर दूसरे method के लिए invisible है, उसी class में भी -- यह isolation methods को गलती से एक-दूसरे की local state में interfere करने से रोकता है।
उदाहरण: Variable Scope
public class Main {
static void methodA() {
int x = 5;
System.out.println(x);
}
public static void main(String[] args) {
methodA();
// x is not visible here
}
}
Login to try C/C++/Java/PHP code in the editor
Why Use Methods?
Repeated logic को एक method में extract करने का मतलब है आप इसे एक बार लिखते और test करते हैं, फिर जहां भी ज़रूरत हो वहां से call करते हैं -- अगर आपको बाद में एक bug fix करना या behavior बदलना हो, आप इसे सिर्फ उस एक जगह edit करते हैं।
उदाहरण: Why Use Methods?
// Define the class Main
public class Main {
static int addTax(int price) {
// Send price + (price / 10) back to the caller
return price + (price / 10);
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(addTax(100));
// Print a line to the console
System.out.println(addTax(200));
}
}
Login to try C/C++/Java/PHP code in the editor
- बिना object के
mainसे एक method call करते समयstaticभूल जाना, जो एक compile error देता है। - किसी method को किसी दूसरे method के अंदर declare करना, जो allowed नहीं।
- एक method define करना लेकिन कभी call न करना, इसलिए यह नहीं चलता।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: