Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
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 }