← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 1 of 10

Java Methods परिचय

एक method steps का एक named set है जो आप बार-बार उपयोग कर सकते हैं, एक recipe जैसा। आप इसे एक बार लिखते हैं और जब चाहें call करते हैं।
Syntax
java
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

java
// 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();
	}
}

Method Declaration Structure

किसी method की declaration पहले से चार चीज़ें specify करती है: इसे कौन call कर सकता है (access modifier), यह किस type की value वापस देता है (return type), इसका नाम, और यह कौन सा data accept करता है (parentheses में parameters)।

उदाहरण: Method Declaration Structure

java
// 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));
	}
}

Flow of Execution

Execution हमेशा main में शुरू होता है; जब भी code किसी दूसरे method की call पर पहुंचता है, control उस method की body में jump करता है, इसे completion तक चलाता है, और फिर original call के बिल्कुल बाद वाली line पर return होता है।

उदाहरण: Flow of Execution

java
// 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");
	}
}

Variable Scope

किसी method के अंदर declared एक variable सिर्फ उस method के execution के लिए मौजूद है और हर दूसरे method के लिए invisible है, उसी class में भी -- यह isolation methods को गलती से एक-दूसरे की local state में interfere करने से रोकता है।

उदाहरण: Variable Scope

java
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
	}
}

Why Use Methods?

Repeated logic को एक method में extract करने का मतलब है आप इसे एक बार लिखते और test करते हैं, फिर जहां भी ज़रूरत हो वहां से call करते हैं -- अगर आपको बाद में एक bug fix करना या behavior बदलना हो, आप इसे सिर्फ उस एक जगह edit करते हैं।

उदाहरण: Why Use Methods?

java
// 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));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना object के main से एक method call करते समय static भूल जाना, जो एक compile error देता है।
  2. किसी method को किसी दूसरे method के अंदर declare करना, जो allowed नहीं।
  3. एक method define करना लेकिन कभी call न करना, इसलिए यह नहीं चलता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.