Member Function (Methods)

  • A member function of a class is a function that has its definition or its prototype within the class definition like any other variable.
  • It operates on any object of the class of which it is a member and has access to all the members of a class for that object.
  • Methods is the preferred term in Java. Member Function is the preferred term in C++.
  • In C language we define functions but they did not belong to any class that's why they called as functions.
  • In Java functions belong to a particular class that's why they called as Member Functions.
  • Member Function are classified into two types static member function and instance member function.
  • In Java Functions / Methods / Member Function / Behavior of an object are the same things.
  • NOTE: We do not write instance while we code beacase if we do not write static or anything so it will consider as instance member function or non-static member function.

Static Member Function

  • Static methods are always which are recommended to perform one operation.
  • Like opening the files, obtaining a data base connection etc.
  • Programmatically static methods definitions must be preceded by static keyword.
  • Each and every static method must be accessed with respect class name.
  • Example:
    public static void main ()
    {
    S1.getTotal();
    }
  • Syntax:
    static ReturnType methodname (parameters if any)
    {
    Block of statements;
    }
  • Static Member Function made changes in static data member and that changes are comman for all class.
  • One static method can call another static method directly provided both the static methods belong to same class.
  • One static method can call another instance method with respective to an object without considering whether the same class or different class.
  1. class Employee
  2. {
  3. static String cname;
  4. String ename;
  5. static void change()
  6. {
  7. cname="HCL";
  8. }
  9. public static void main(String...a)
  10. {
  11. System.out.println(Employee.cname);
  12. Employee.change();
  13. System.out.println(Employee.cname);
  14. }
  15. }
  16. To Compile - javac employee.java
  17. To Run - java Employee
  18. Result - null
    HCL

Instance Member Function

  • Instance methods are always recommended to perform repeated operations.
  • Like reading records from the data base etc.
  • Programmatically instance methods definitions should not be preceded by a keyword static.
  • Each and every instance method must be access with respective Object name.
  • Example:
    void getTotal()
    {
    Tot= m1+ m2+ m3;
    }
  • Syntax:
    ReturnType methodname (parameters if any)
    {
    Block of statements;
    }
  • The changes made by Instance Member Function are separate for each object.
  • One instance method can call another instance method directly provided both the instance methods belong to same class.
  • One instance method can call another static method with respective to class name without considering the same class name or different class name.
  1. class Employee
  2. {
  3. static String cname="HCL";
  4. String ename;
  5. void change(String e)
  6. {
  7. ename=e;
  8. Systme.out.println(ename);
  9. }
  10. public static void main(String ...a)
  11. {
  12. Employee e1=new Employee;
  13. System.out.println(e1.ename);
  14. e1.change("EduEye");
  15. }
  16. }
  17. To Compile - javac employee.java
  18. To Run - java Employee
  19. Result - null
    EduEye

| Copyright ©2016 | All Rights Reserved |
| Design by Uves Khan |

Free Web Hosting