Previous | Home | Next |
There are two types of member methods in java
- Instance Member Methods/Non-Static member Methods.
- Static Member Methods.
Instance Member Methods
- Instance method is defined in a class which is only accessable through the Object of the class are called Instance method.
- These methods can only be called through objects.
- Non-static member are declared without the static keyword.
- If we call a instance method inside a static method of same class then also we have to use object name.
- When an object calls a method then it is called as message passing and this is feature of oops.
- When an object calls a method then the referrence of the object is automatically passed as an argument to the method and this reference will be received by this keyword inside the class.
Syntax of instance method
return-type funcName(paramList) { // Body of this method }
return-type is the type of answer that the method produces.
Syntax to call instance method
objRef.funcName(args);
objRef is an object reference variable and funcName is the name of the method. args are an optional arguments.
Example :
class Instance { int l = 9; //non-static double d = 12.2;//non static void sample() //non-static method { System.out.println("hello"); } public static void main(String args[]) { Instance i = new Instance();//instance creation System.out.println(i.l); System.out.println(i.d); i.sample(); System.out.println("program ends...."); } }
output :
9 12.2 hello Program end....
Static Member Methods
- These methods can be called through class name or through object name.
- Static methods can access ony static member. Static method can be called directly (without object or class name) from a static method of same class.
- This keyword will not work inside a static method because this contains the reference of the calling object but static method is generally called through class name and even if static method is called through object then also the reference of calling object will not passed in the method.
- Super keyword will also not work.
- Instance member variables can not work directly inside a static method but using object we can access in instance member also.
- static method can use only static variables.
Example1 :
class A { int x; // instance member variables static void m() { x=5; System.out.print(x); } public static void main(String args[]) { A.m(); } }
output :
Error - Static method can not access instance variable
Example2 :
class A { int x; //instance variable static void m() { A a1=new A(); a1.x=5; System.out.print(a1.x); } } class StaticMethod { public static void main(String args[]) { A.m(); } }
Output :
5
Example3 :
class Math { static int sum(int a, int b) { return(a+b); } static float avg(int a, int b) { return((a+b)/2.0f); } } class mathMathod { public static void main(String args[]) { System.out.println(Math.sum(5,6)); System.out.println(Math.avg(5,6)); Math m=new Math(); System.out.println(m.sum(5,6)); System.out.print(m.avg(5,6)); } }
output :
11 5.5 11 5.5
Previous | Home | Next |