Translate

Sunday, March 29, 2020

Method Concept in Java

Syntax of methods in JAVA
{
  • Syntax    :-  modifierList returnType methodName(parameterList)
  • Example :- public void Addition(int a, int b){.....Logic.....}
}


Types of Methods in JAVA
{
  1.  Static Method :-
  2.  Instance Method.
}


Definition of Methods
{
  • Static     :- Static method in java belongs to class and not its instance is called Static method.  
  • Instance  :- This method are methods which required an object of its class to be created before it can be called. Such methods are called Instance method.

}

Examples of Methods

NOTE : Following example is very important for destroying confusion in methods concept in JAVA.

  • Example 1 : How to call methods 
class Test             
{
      void m1()  // Instance Method
      {
            System.out.println("Instance Method");
            new Test().m2();  // possible :- By objectName                       
            m2();          // possible :- By Directly
            Test.m1();  // possible :- By ClassName
      }
   
      void  m1()  //not possible :- Error
      {

      }

      static void m2()  // Static Method
      {
            System.out.println("Static Method"); 
      }
   
      m3()       //Not possible :- Error
     {
            System.out.println("No returnType"); 
            
     }
     ðŸ‘‡ // Main Method as well
      public static void main(String args[])     
      {
             Test t = new Test();    //Object Creation
           
             t.m1();  // possible :- 
             Test.m1();    // not possible :- Error 
             
             m2();                   //possible :-               
             t.m2();                 //possible :-
             Test.m2();           //possible :- 
      }
      /*
      Output :-
       Instance Method
       Static Method
       Static Method
       Static Method
       Static Method
       Static Method
       Static Method
      */
   
}

Note 1 :- It is possible to call instance method inside the static method by using object only.
Note 2 :- It is possible to call static method inside the static method or instance method by using Class which is recommended by good developers but we can also called directly and by object.
Note 3 :- It is not possible to make more then one method with same name with same parameter.
Note 4 :- Return type of method is mandatory otherwise J.V.M. will give error.

  • Example 2 : Calling Methods with parameters
class Test
{
      👇// instance method with parameters
      void m1(int a, char x)   
      {
            System.out.println(a);
            System.out.println(x);
            void m2()               //Not Possible :- Error 
            {
                   System.out.println("Inner method");
            }
      }
      👇 // static method with parameter
      static void m2(int b, char y)       
      {
            System.out.println(b);
            System.out.println(y);
      }
   
      public static void main(String args[])
      {
            👇// passing parameter in instance method.
            new Test().m1(7, 'K');         
           
            Test.m2(18, 'V');                 
            👆// passing parameter in static method.
      }
      /*
      Output :-  
       7
       K
       18
       V
      */
}
Note 1 :- We should pass values according to method parameters dataType.
Note 2 :- Java not support inner method but java allowed to create inner class.

  • Example 3 : Passing object as a value to method.
class X{}
class Y{}
class Emp{}
class Std{}
class Test
{
      //Here class X works as a Datatype and object x as a variable
      void m1(X x, Emp e)          
      {
            System.out.println("Instance m1 method");
      }
      //Here class Std works as a Datatype & object s as a variable. 
      static void m2(Y y, Std s)   
      {
            System.out.println("Static m2 method");
      }

      public static void main(String args[])
      {
             System.out.println("Main Method");
             Test mainObj = new Test();     
             X xObj = new X();
             Y yObj = new Y();
             Emp eObj = new Emp();
             Std sObj = new Std();
             ðŸ‘‡//passing objects as a parameter.
             mainObj.m1(xObj, eObj);     
           
             Test.m2(yObj, sObj);           
             ðŸ‘†//passing objects as a parameter.
      }
      /*
      Output :-
       Main Method
       Instance m1 method
       Static m2 method
      */
}
Note :- We can also passing object as a value where method parameters dataType is Object.

  • Example 4 : Use of "this" keyword in Method.
class Test
{
     int a = 20, b = 30;
     void m1(int a, int b)  //possible
     { 
            System.out.println(a+b);
            👆//local variable output 444 
            System.out.println(this.a+this.b); 
            ðŸ‘†//Instance variable o/p 50
     }
     static void m2(int a, int b) //static method            
     {
            System.out.println(this.a+this.a); 
            👆 //Not possible :- Error
            Test t = new Test();
            System.out.println(a+b);     
           ðŸ‘† //local variable output 888
            System.out.println(t.a+t.b);     
           ðŸ‘† // Instance variable ouput 50 
     }
     public static void main(String args[])
     {
            Test obj = new Test();
           obj.m1(123, 321);
           obj.m2(345, 543);
     } 
}
Note 1 :- In Java "this" keyword use for accessing instance variable in instance method.
Note 2 :- It give Error when we use "this" keyword inside the static method.

No comments:

Post a Comment

Constructor in Java

Rules of Constructor Class name and constructor name must be same Constructor is able to take parameters Constructor is not allowed re...