Method reusable piece of code या block of code होता है जो कि कोई specific task perform करता है। एक बार define करने के बाद हम इन्हें script में कितनी ही बार use / call कर सकते हैं।


हालाँकि Java हमें ये facility provide करती है कि user खुद के function define कर सके जिन्हें User Defines Functions कहते हैं । Web page load होते समय कोई भी function automatically run नहीं होता है जब तक कि हम उसे manually call / invoke न करें।

Java Method Declaration

function declaration को function definition या function statement भी कहते हैं , बाकी languages like PHP, JavaScript की तरह Java में method define करने के लिए specifier, static / non static और return type के बाद method name define कर दिया जाता है।

visibility static/non-static returntype method_name()
{
  //perform task here
  return value according to returntype.
}
  • Visibility : Class में methods define करने से पहले उनकी Visibility भी define करनी पड़ती है , visibility means , class के अंदर define किये जाने वाले methods को कहाँ कहाँ access कर सकते हैं और कहाँ नहीं। यह 4 types की होती है - public , protected , private और by default , default ही होती है। हालाँकि उन्हें कैसे use में लेते हैं वो , आगे पढ़ेंगे।

  • static / non-static : class में methods 2 types के हो सकते हैं -

    1. Static Methods - इन्हे हम बिना Object initiate किये भी Class name से access कर सकते हैं , लेकिन class के अंदर आप इन्हे directly (without class name) भी call कर सकते हैं। static member define करने के लिए static keyword का use किया जाता है।

    2. Non Static members - इन्हे हम Object initiate किये बिना access नहीं कर सकते हैं। By Default define किये गए methods का type Non Static ही रहता है।

  • return type : method define करते समय हमें उसका return type define करना होता है , जो define करता है कि method किस type की value return करेगा। अगर function कोई value return नहीं करता तो , आप void type declare कर सकते हैं।

  • method_name : कोई भी valid name हो सकता है , जो कि string या underscore के साथ start हो और Java में predefined keywords से match नहीं करता हो। Java में method name number से start नहीं होता है , numbers को function name के बीच में या last में दे सकते हैं। But कहीं भी floating point numbers नहीं दे सकते हैं।

  • return statement, method run होने के बाद method declaration के type के according value return करता है।

Java Method Example

File : MethodExample.java

CopyFullscreenClose FullscreenRun
public class MethodExample {
  // define method.
  static void my_method() {
     System.out.println("Test Method..");
  }

  public static void main(String[] args) {
    my_method();
  }
}
Output
javac MethodExample.java
java MethodExample
Test Method..

हालाँकि इस method को आप class name के साथ भी access कर सकते हैं।

MethodExample.my_method()

need के according आप इसे कई बार call कर सकते हैं।

File : MethodExample.java

CopyFullscreenClose FullscreenRun
public class MethodExample {
  // define method.
  static void my_method() {
     System.out.println("Test Method..");
  }

  public static void main(String[] args) {
    my_method();
    my_method();
    my_method();
  }
}
Output
javac MethodExample.java
java MethodExample
Test Method..
Test Method..
Test Method..

Java Method With return type

जैसा कि आपने देखा कि जब method से कोई भी value return नहीं होती है , तो हम void लिखते हैं और अगर कोई value return होती है तब उसका return type define करना पड़ता है।

File : MethodExample.java

CopyFullscreenClose FullscreenRun
public class MethodExample {
  // now define with return type.
  static String my_method() {
     // return any string value.
     return "Test Method..";
  }

  public static void main(String[] args) {
    String message = my_method();
    System.out.println(message);
  }
}
Output
javac MethodExample.java
java MethodExample
Test Method..

Java Advantages Of Methods

  1. Code Re-usability : methods use करने का सबसे बड़ा advantage यही है कि , हम code को reuse कर सकते हैं। same processing के लिए एक बार method define करने के बाद उसे हम कही भी और कितनी बार भी use कर सकते हैं।

  2. Less Code : चूंकि हम same code के लिए methods use करते हैं जिससे Program की length कम जाती है।

  3. Reduce Coding Time : method use करने से coding time reduce होता है , जो कि किसी भी developer के लिए important है।

  4. Easy To Understand : Code को समझना आसान हो जाता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers