Method
Method in JAVA
Adding Methods to Classes
Methods are declared inside the body of the class but immediately after the declaration of instance variables. The general form of a method's declaration isReturntype methodsName (parameter)
{
// Method_body;
}
- A return type can be a primitive type such as int, a class type such as string or void.
- A method name begins with a lowercase letter and compound words in the methods name should begin with uppercase letter according to Java convention.
- An optional parameter list/argument list must be inside parentheses, separated by commas.
- The method-body must be enclosed in braces.
Return Type
The return type specifies the type of value the method would return. This could be a simple data type such as int as well as any class type or it could even be void type if the method does not return any value.
Example:
/**
*
* @author Suraj Pratap Singh
*
*/
class Demo{
double width;
double height;
double depth;
/*This method calculate the volume*/
double volume(){
return width*height*depth;
}
}
/**
*
* @author Suraj Pratap Singh
* ImplNote : This is the Main class
*/
class Test {
public static void main(String arg[])
{
Demo demoObj=new Demo();
double vol;
demoObj.width = 5;
demoObj.height = 25;
demoObj.depth = 15;
vol = demoObj.volume();
System.out.println("Volume is : "+vol);
}
}
The type of the data returned by a method must be compatible with the return type specified by the method.
The variable receiving the value returned by a method must also be compatible with the return type specified for the method.
Method Accepting Parameters
The parameter list is always enclosed in parentheses. This list contains variable names and types of all the values we want to give to the method as input. If the method does not take any argument, simply leave the parentheses empty.
/**
*
* @author Suraj Pratap Singh
* ImplNote : This Class is for Rectangle
*/
class Test {
int length;
int width;
/*This method takes the data as parameter x and y*/
void getData (int x, int y)
{
length = x;
width = y;
}
int rectArea()
{
int area = length * width;
return (area);
}
}
In the above example, length and width are instance variables.
- x and y are parameters.
- void indicates method, does not return any value.
When a primitive is passed to a method, a copy of the value is generated. If the method changes the value of the argument in any way, the only local argument will be affected. When the method terminates, this local argument is discarded and the original variable in the calling method is unchanged.
/**
*
* @author Suraj Pratap Singh
* ImplNote : This Class is for Rectangle
*/
class Test {
int length;
int width;
void change (int l, int w)
{
l = l + 10;
w = w + 10;
System.out.println("l : "+l+" w: "+w);
}
public static void main(String arg[])
{
int len=10;
int thick=20;
Test testObj=new Test();
testObj.change(len, thick);
System.out.println("len: "+len+" thick: "+thick);
}
}
Accessing Class members
/**
*
* @author Suraj Pratap Singh
*
*/
class Rectangle{
int length, width;
void getData(int x, int y)
{
length = x;
width = y;
}
int rectarea( )
{
int area = length * width;
return (area);
}
}
class Test
{
public static void main(String arg[])
{
int area1, area2;
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
rect1.length = 15;
rect1.width = 10;
area1 = rect1.length * rect1.width;
rect2.getData(30, 13);
area2 = rect2.rectarea();
System.out.println("Area1 : "+area1);
System.out.println("Area2 : "+area2);
}
}
Output:
Area1 : 150
Area2 : 390
Use the dot operator with instance variable and to call instance methods. The general syntax is
Objref.instance variable;
Objref.MethodName (argument);
Read below posts.Most Frequently Asked Java Interview Programs. PART-1Most Frequently Asked Java Interview Programs PART-2
Comments
Post a Comment