In Java methods have so much power and flexibility as all the operations done by methods.

The general form of method is

type method_name(parameter-list)

{

// body of method

}

type- It is the type that returned by the method like int, float etc. also class type which we already created. if there is no value to return in the method then it is void.

method_name- It is the simply name of the method which is not already existing identifier and keywords.

parameter-list- These are the sequence of type identifier followed by comma which we have to pass to the methods when we calls the method. It can be empty if there is no need to pass values while calling the method.

return value; -This will returns the value of the type which we declare method of type.

Adding a method to Rectangle class

Methods are used to access the data inside the class which provides the hiding of data means abstraction.

//example for adding method to class
class Rectangle
{
double length;
double breadth;
  void area()
  {
    System.out.print("area is::");
    System.out.println(length*breadth); // no need of dot operator as it defined within the same class
  }
}
public class RectangleDemo{

public static void main(String []args)
{
double area;
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle();

rectangle1.length=10.0;
rectangle1.breadth=3.5;
rectangle2.length=11.0;
rectangle2.breadth=4.0;

rectangle1.area(); //calls area() method for rectagle1 object.
rectangle2.area(); //calls area() method for rectangle2 object.
}
}

Output:

area is::35.0
area is::44.0
+

Returning a Value

we can use methods that will return a value of type which we used type when declaring a method.

//example for a method which returns a value
class Rectangle
{
double length;
double breadth;
  double area()
  {
    return length*breadth;
  }
}
public class RectangleArea{

public static void main(String []args)
{
double area;
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle();

rectangle1.length=10.0;
rectangle1.breadth=3.5;
rectangle2.length=11.0;
rectangle2.breadth=4.0;

  area = rectangle1.area(); // the assigning variable and the return value type should be same.

  System.out.println("Area of Rectangle1 is::"+area);

System.out.println("Area of Rectangle2 is::"+rectangle2.area()); //inside a println() method.
}
}

Output:

Area of Rectangle1 is::35.0
Area of Rectangle2 is::44.0

Adding a Method That Takes Parameters

It is better to use parameterized methods as it performs on different values of same operation.

Two uses

  • Reduces clumsy and error prone.
  • well designed java program as instance variables are accessed by only its methods.
//example for using methods which takes parameters
class Rectangle
{
double length;
double breadth;
  void setParameters(double l,double b)
  {
    length =l;
    breadth = b;
  }
  double area()
  {
    return length*breadth;
  }
}
public class RectangleArea{

public static void main(String []args)
{
double area;
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle();

rectangle1.setParameters(10.0,3.5);
rectangle2.setParameters(11.0,4.0); 

  area = rectangle1.area();

  System.out.println("Area of Rectangle1 is::"+area);

System.out.println("Area of Rectangle2 is::"+rectangle2.area());
}
}

Output:

Area of Rectangle1 is::35.0
Area of Rectangle2 is::44.0

results matching ""

    No results matching ""