Member function in Java

0
It is used to define the functionality of the object using a separate block of code.
Member function provides subroutine or submodule to define different functionality underclass.

Type of Member Function:-

1.0 According to Memory allocation

1.1.1 Static Member function:- 

This type of member function will be called by Classname and it is used to define static data member of the class.

we will use static modifier in front of the function name to declare a static member function.

access specifier static return type methodName()
{


}

public static void display()
{
     statement1;
     statement2;
     statement3;
}

class Addition
{
   static int a;
   static int b;
   static int c;
   static void add()
   {
      a=100;
      b=200;
      c=a+b;
      System.out.println(c);
   }
   public static void main(String args[])
   {
       Addition.add();   

   }


}
....................................................................
Static Program Using User Input:-
import java.util.Scanner;
class Addition
{
   static Scanner sc = new Scanner(System.in);
   static int a;
   static int b;
   static int c;
   static void add()
   {
      System.out.println("Enter first number");
      a=sc.nextInt();
      System.out.println("Enter second number");
      b=sc.nextInt();
      c=a+b;
      System.out.println(c);
   }
   public static void main(String args[])
   {
       Addition.add();   
      Addition.add();      //add()
      System.out.println(Addition.a);


   }


}
.....................................................................................................

1.1.2  Dynamic Member function:-

This type of member function is used to define the value of dynamic data member of the class. dynamic member function will be called by Object.

return type Methodname()
{
      return data;
}

..........................................................................................................
import java.util.Scanner;
class Addition
{
    Scanner sc = new Scanner(System.in);
    int a;
    int b;
    int c;
  void add()
   {
      System.out.println("Enter first number");
      a=sc.nextInt();
      System.out.println("Enter second number");
      b=sc.nextInt();
      c=a+b;
      System.out.println(c);
   }
   public static void main(String args[])
   {
      Addition obj = new Addition();
      obj.add();
      Addition obj1 = new Addition();
      obj1.add();
      System.out.println(obj1.a); //a value will be different
      System.out.println(obj.a); //a value will be different
     


   }


}



2.0 According to Parameter:-

Discuss in another day





Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)