Advertisement

How to Create Methods in Java in Hindi – कोड को रीयूज़ (Reuse) कैसे करें?

हेल्लो दोस्तों, हमारी Java Programming सीरीज में आपका स्वागत है। पिछली पोस्ट में हमने Arrays के बारे में पढ़ा था। आज हम कोडिंग की दुनिया का एक ऐसा जादुई टूल सीखेंगे जो आपकी मेहनत को आधा कर देगा – Methods in Java in Hindi (मेथड्स)।

सोचिये, अगर आपको दिन में 10 बार चाय बनानी हो, तो क्या आप हर बार गैस जलाएंगे, दूध डालेंगे, चीनी डालेंगे? या फिर एक बार बनाकर थर्मस में रख लेंगे? Methods का काम भी यही है – "कोड को एक बार लिखो और हजार बार इस्तेमाल करो" (Write Once, Use Anywhere) ।

इस पोस्ट में हम जानेंगे कि Java में Method क्या है?, इसे कैसे बनाते हैं और Parameters और Return Type का क्या मतलब है?

Advertisement

What is Method in Java in Hindi? - जावा में मेथड क्या है?

Method कोड का एक ब्लॉक (Block) होता है जो केवल तभी चलता है जब उसे बुलाया (Call) जाता है।

इसे दूसरी भाषाओं (जैसे C/C++) में Function कहा जाता है। Java में चूंकि सब कुछ Class के अंदर होता है, इसलिए हम इसे Method कहते हैं।

फायदे:

Advertisement
  • Code Reusability: आपको एक ही कोड बार-बार लिखने की जरुरत नहीं है।
  • Readability: कोड साफ़-सुथरा और समझने में आसान हो जाता है।

The Juicer Analogy (आसान उदाहरण)

मेथड को आप एक जूसर मशीन (Juicer) की तरह समझ सकते हैं:

  1. Input (Parameters): आप जूसर में फल (Fruits) डालते हैं।
  2. Processing (Body): मशीन चलती है और फल को पीसती है।
  3. Output (Return Type): मशीन आपको जूस (Juice) बनाकर देती है।

अगर मशीन जूस नहीं देती (सिर्फ कचरा फेंकती है), तो उसे हम void कहते हैं।

Syntax of Method in Java in Hindi - जावा में मेथड बनाने का तरीका

मेथड को डिक्लेअर करने का एक खास स्ट्रक्चर होता है:

AccessModifier ReturnType MethodName(Parameters) {

// code

}

  • Access Modifier: जैसे public, private (कौन इसे यूज़ कर सकता है)।
  • Return Type: यह क्या वापस देगा? (int, String, या कुछ नहीं तो void) ।
  • Method Name: इसका नाम (जैसे addNumbers, makeTea)।

Types of Methods in Java in Hindi - जावा में मेथड्स के प्रकार

Java में दो प्रकार के मेथड्स होते हैं:

  1. Pre-defined Methods: जो Java ने पहले से बनाये हैं (जैसे System.out.println(), Math.sqrt())।
  2. User-defined Methods: जो हम (प्रोग्रामर) अपनी जरुरत के हिसाब से बनाते हैं।

Java Method Example Code

चलिए एक मेथड बनाते हैं जो दो नंबरों को जोड़ता है।

class MethodExample {

static void sayHello() {

System.out.println("Hello User!");

}

static int add(int a, int b) {

return a + b;

}

public static void main(String[] args) {

sayHello();

int result = add(5, 10);

System.out.println("Sum is: " + result);

}

}

Output:

Hello User!

Sum is: 15

Return Type & Parameters Concept in Java

1. Parameters (Arguments)

यह वो वैल्यू है जो हम मेथड को देते हैं (जैसे जूसर में फल डालना)। ऊपर वाले एग्जांपल में int a और int b पैरामीटर्स हैं।

2. Return Value

यह वो रिजल्ट है जो मेथड हमें वापस देता है।

अगर मेथड कुछ वापस करता है (जैसे return a + b), तो हमें उसका टाइप (जैसे int) लिखना पड़ता है।

अगर मेथड कुछ वापस नहीं करता, तो हम void लिखते हैं।

Conclusion – निष्कर्ष

हमें उम्मीद है कि इस पोस्ट से आपने Methods in Java in Hindi को अच्छे से समझ लिया होगा।

मेथड्स प्रोग्रामिंग की जान हैं। इससे आप बड़े से बड़े सॉफ्टवेयर को छोटे-छोटे टुकड़ों में बाँट सकते हैं।

अगली पोस्ट में हम Java के सबसे बड़े और डरावने टॉपिक की शुरुआत करेंगे – OOPs Concepts (Object Oriented Programming) । लेकिन डरें नहीं, हम इसे बहुत आसान बना देंगे!

FAQs

Q1. Function और Method में क्या अंतर है?

Ans: दोनों काम एक ही करते हैं। अगर कोई फंक्शन किसी Class के अंदर है, तो उसे Method कहते हैं। Java में सब कुछ क्लास में होता है, इसलिए यहाँ सिर्फ मेथड्स होते हैं।

Q2. static मेथड क्या है?

Ans: अगर हम चाहते हैं कि मेथड को कॉल करने के लिए ऑब्जेक्ट न बनाना पड़े, तो हम static लगाते हैं। (जैसे main मेथड)।

Q3. क्या एक मेथड दूसरे मेथड को कॉल कर सकता है?

Ans: हाँ, बिल्कुल! इसे Method Calling कहते हैं। अगर कोई मेथड खुद को ही कॉल करे, तो उसे Recursion कहते हैं।

Table of Contents

Close

Comments

Share to other apps

Report Content

Why are you reporting this content?

Your selection helps us review the content and take appropriate action.

Hate & Discrimination
Content that spreads hate or unfair treatment against a person or group because of who they are.
Abuse & Harassment
Content that insults, threatens, bullies, or makes someone uncomfortable.
Violence & Threats
Content that talks about hurting people, animals, or property, or supports violence.
Child Safety
Any content that harms, exploits, or puts children at risk.
Privacy Violation
Sharing someone’s personal information or photos without permission.
Illegal & Regulated Activities
Content that promotes or helps with illegal activities like drugs, weapons, or trafficking.
Spam & Misleading Content
Fake, misleading, or repeated content meant to trick users.
Suicide or Self-Harm
Content that encourages or explains self-harm or suicide.
Sensitive or Disturbing Content
Shocking or graphic content that may upset users.
Impersonation
Pretending to be another person or organization.
Extremism & Hate Groups
Content that supports violent groups or hateful ideas.
Civic Integrity
Content that spreads false information about elections or public processes.