sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » Java Methods

हर Java के Program में Main Method का इस्तेमाल किया जाता है |
C, C++ में जो function इस्तेमाल किये जाते थे, वैसे ही Java में उसे Methods कहा जाता है |
Java के Methods में अनेक से statements grouped करके रखे जाते है |
Java में अपने खुदके Methods भी बनाये जाते है | Methods parameters, बिना parameters, return type और बिना return type से create किये जाते है |

Java Main Method()

class Sample{
 
 public static void main(String args[]){   // main method
 
 //statements;
 
 }
}

Syntax for Java Method

visibility return_type method_name(arguments_list){
 
 //method's statments;
}

Example for Java Method

दिए हुआ example parameter के बिना है |
Java Methods चार हिस्सों में होता है |
  1. Visibility : Method की accessibility कहा तक होनी चाहिए, ये visibility modes बताते है | इसे access specifier या access modifier भी कहते है |Java में visibility के तीन प्रकार होते है |
    1. private : private methods सिर्फ अपने class के लिए ही सिमित होते है | class के बाहर या किसी दुसरे class में इन्हें access नहीं किया जाता |
    2. public : puublic methods को किसी भी class पर access किया जाता है |
    3. protected : protected methods; inheritance या अपने sub-classes के साथ कहा पर भी access किये जाते है |
  2. return_type : ये method का return type है | जब method को कोई return type नहीं दिया जाता, तो 'void' इसका default type होता है |
  3. method_name : Method का नाम statements से related कुछ भी हो सकता है | Method का नाम कोई keyword नहीं होता |
  4. arguments_list : Methods के parameters को () parenthesis के अन्दर लिखे जाते है | ये program की requirement के हिसाब से दिए जाते है |
Example में देखे,
visibility : public
return_type : int
method_name : add
arguments_list : No Argument
public int add(){
 
 int a, b, c;
 a = 50; b = 60;
 c = a + b;
 return c;
}

Method With Arguments

Source Code :
public int add(int x, int y){   //x and y is Method Arguments
 
return x + y;
}

Method Calling

जब Method को define किया जाता है तभी उसको call किया जा सकता है | कुछ methods pre-defined होते है | जिनकी definition; packages में लिखी जाती है |

Example for Pre-defined Method

यहाँ पर PrintStream class की println ये method इस्तेमाल की गयी है | ये method java.io इस package के अन्दर defined की गयी है | यहाँ पर println method; call की गयी है |
class Sample{

 public static void main(String args[]){
 
 Sample s = new Sample();
 System.out.println("Hello")); // println is predefined method
 }
}
Output :
Hello

Method को return value के साथ या बिना return value के साथ call किया जाता है |
Method को call किसी return type के variable में भी store किया जाता है |
For eg.
int a = add();

Full Example for Method Calling

Source Code :
class Sample{
 
 public int add(int x, int y){     // Non-Static Method
 
 return x + y;
}
 public static void main(String args[]){
 
  Sample s = new Sample();
  int a = s.add(5, 9);
  System.out.println(a);
}
}
Output :
14

Using Static Method

जब static का इस्तेमाल method में किया जाता है तो class के object के बिना method को access किया जाता है |
For eg.
Source Code :
class Sample{
 
 public static int add(int x, int y){  // static method

 return x + y;
}
 public static void main(String args[]){

  int a = add(5, 9);
  System.out.println(a);  //or System.out.println(add(5, 9));
}
}
Output :
14

Method Overloading

Java में एक से ज्यादा एक जैसे नाम के method को इस्तेमाल किया जाता है |
एक java program में एक से ज्यादा एक ही नाम के method इस्तेमाल करना 'Method Overloading' कहते है |
एक method दूसरी बार पहले जैसा इस्तेमाल नहीं किया जा सकता | For Example,
जब ऐसा होता है तो, method is already defined in class ये error आ जाता है |
public void disp(){}
public void disp(){}
Method Overloading का इस्तेमाल करना हो तो, हर same name के method का parameter का data_type, parameters की संख्या अलग-अलग इस्तेमाल किया जाता है | For Example,
void disp(){ }
void disp(int a){ }
void disp(int a){ }
void disp(int a, int b){ }
void disp(int a, int b, int c){ }

Example for Changing Data Type : Method Overloading

Source Code :
class Sample{
  
  public void disp(double x){
   System.out.println(x);
  }
  public int disp(int x){
   return x;
  } 
 
 public static void main(String[] args){
 
 Sample s = new Sample();
 
 System.out.println("Value of x : " + s.disp(5));
 System.out.println("Value of y : " + s.disp(6));
 
 }
}
Output :
Value of x : 5
Value of y : 6

Example for Changing number of Arguments : Method Overloading

Source Code :
class Sample{
  
  public void disp(int x, int y){
   System.out.println("Value of x : " + x);
   System.out.println("Value of y : " + y);
  }
  public void disp(int x){
   System.out.println("Value of x : " + x);
  } 
 
 public static void main(String[] args){
 
 Sample s = new Sample();
 
 s.disp(5, 6);
 s.disp(7);
 
 }
}
Output :
Value of x : 5
Value of y : 6
Value of x : 7

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply