You are on page 1of 14

METHODS

Methods are same as functions or procedure Method signature includes Name of method , parameters and return type Examples void getdata(int a , float b , String c); void showdata(); A method except constructor method must always have a return type which can be either void or any of basic data types or a class name

Methods
Method definition : It includes the code what a method is supposed to do or execute Syntax is void getdata(int a , int b) { // code } Calling a method : Example: getdata(c,d); here the data type of c and d should match with a and b

METHOD OVELOADING
IN ONE CLASS WE MAY HAVE MORE THAN ONE METHOD WITH THE SAME NAME , BUT WITH DIFFERENT PARAMETERS WHICH MAY VARY IN QUANITY, DATA TYPES OR SEQUENCE FOR EXAMPLE THE FOLLOWING METHODS ARE OVELAODED METHODS void getdata(int a, int b); int getdata(float x , int y); float getdata(int a, int b,int c); All the above methods are having different signatures

Method overloading Example of same signatures


Following methods have a same signatures void getdata(int a, int b); void getdata(int c, int d); int getdata(int a, int b) have same signatures as such these not be oveloaded and if we include more than one of these in the same class it shall give an compilation error It may be seen the methods with same parameters quantity sequence and data types but only different return type can not be allowed as these are considered be having same signatures

Constructor
Constructor is a method which has the same name as the class name and without any return type for example method with signature : student(); student(int a); student(int a , float y) are all constructor method of class student with different signatures If we include a return type before the method having a same name as class name than it is not considered to be a constructor method but a normal method and can be called like any other method Constructor methods are automatically called when object is created

Constructors
We may have two methods in class student with following signatures int student() and student() ,here the second method is the constructor method and first method is not a constructor method however if we have a third method as float student() than there is a conflict between int student() and float student() as such it is not allowed

Constructors
Constructors method are automatically called when the object is created For example when we create an object student y= new student(); than constructor with signature student() is called if we create an object student d = new student(x,y); where x and y are two int variables than constructor with signatures student(int,int) shall be called however we must have included (defined) this method in our class

Return key word


return key word is used in a method a method which has as return type must return a value of same type as return type If the return type is void than we may or may not used return statement in method body it has to be just return; (should not be returning) any value When a method encounters a return statement the control goes back to calling method and also returns a value if it is not of void return type

Simple example using return


public class test3 { public static void main (String h[]) { int k; k= sum(3,4); System.out.println(k); } static int sum (int a,int b) { return a+b ; } } Value 7 shall be returned which shall be received by k , in statement k= sum(3,4); , first sum method is called and the value retuned by method is received by k

public class test3 { public static void main (String h[]) {

Return

int k; k= sum(3,4); System.out.println(k); k=sum (-1,2); System.out.println(k); }

static int sum (int a,int b) { if( a< 0 || b < 0) return 0; return a+b ; }

This keyword
this key word has the address of the object which is calling the method We may or may not prefix the keyword this with class variables used in class(ie non static method) When ever a method is called Java internally sends the object reference through this key word to the method being called so that method knows which object has called it The use of keyword along with static variable or instant variable shall result in a compiler error

This example
public class testthis { int r; static int y; static int method1() { // this.r=20; not allowed // r=20 //not allowed return 4; } void method2() { r=23 // we can use r or this.r in class method (non sttaic method) this.r=23; } public static void main(String h[]) {

}
}

This keyword
public class test3 { int k; public static void main (String h[]) { test3 y = new test3(); y.test(); System.out.println( y.k); } void test() { this.k=20; } }

This
In some situations we have required to use this key word for example class student { Int x,y int method1(int x,int y) { this.x= x+20;// here in this.x refer to class Variable // and x refers to instant variable }

You might also like