Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Exploring Methods in Java

 

In this lesson, you will learn

  • Adding Method that Takes a Parameters
  • Set the value of the instance variable using a method
  • Examples

 

Adding Method that Takes a Parameters

Here is a method square() that returns the square of the number 10.

int square()
{
      return 10*10;
}

 

The above method will return only the square of the number 10 always. Its use is very limited. We have to create a general-purpose method that can calculate the square of any number.

 

You can add a parameter to the above method. Here is the modified version of method square().

int square(int i)
{
      return i * i;
}

 

Now, square(int i) will return the square of whatever value it is called with.

 

Example

int x=square(10); // x equals 100
int y=square(15); // y equals 225

 

Example 1: Adding a Parameters to a Method

package ch7;
 
class Calculator {

    int add(int a, int b) {
        return a + b;
    }
 
    int multiply(int a, int b) {
        return a * b;
    }
}
 
public class CalculatorMain {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println("Addition: " + calc.add(5, 3));
        System.out.println("Multiplication: " + calc.multiply(4, 2));
    }
}

Output

Addition: 8
Multiplication: 8

 

The add() method is declared with two parameters of type int. This method calculates the addition of two numbers using the formula a+b and returns the result.

 

Example 2: Setting the Value of Instance Variable using a Method

The following examples demonstrate an instance method to initialize instance variables.

public class Rectangle {
    double length;
    double width;

    void setDimensions(double newLength, double newWidth) {
        length = newLength;
        width = newWidth;
    }

    double calculateArea() {
        return length * width;
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle rect1 = new Rectangle();
        rect1.setDimensions(5.0, 3.0);

        Rectangle rect2 = new Rectangle();
        rect2.setDimensions(6.0, 4.0);

        System.out.println("Area of rect1: " + rect1.calculateArea());
        System.out.println("Area of rect2: " + rect2.calculateArea());
    }
}

Output

Area of rect1: 15.0
Area of rect2: 24.0

 

Example 3: Book Class Example

public class Book {
    String title;
    int pages;

    void setTitle(String newTitle) {
        title = newTitle;
    }

    void setPages(int newPages) {
        pages = newPages;
    }

    void displayInfo() {
        System.out.println("Title: " + title + ", Pages: " + pages);
    }
}

public class Main {
    public static void main(String[] args) {
        Book book1 = new Book();
        book1.setTitle("Java Programming");
        book1.setPages(500);

        Book book2 = new Book();
        book2.setTitle("Effective Java");
        book2.setPages(300);

        book1.displayInfo();
        book2.displayInfo();
    }
}

Output

Area of rect1: 15.0
Area of rect2: 24.0

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent100%
Very good0%
Average0%
Poor0%
Terrible0%

 

 

30/10/2025

Very nice content.

 

 

Submit a Review