WikiGalaxy

Personalize

Java Class Methods

Understanding Java Methods:

Methods in Java are blocks of code that perform specific tasks and are defined within a class. They are used to implement the behavior of objects created from a class.

Defining a Method:

A method must have a return type, a name, and optionally parameters. It is defined using the syntax: returnType methodName(parameters).

Calling a Method:

Methods are called by their name. If they have parameters, you pass the arguments in parentheses.


class Car {
  void startEngine() {
    System.out.println("Engine started.");
  }

  public static void main(String[] args) {
    Car myCar = new Car();
    myCar.startEngine();
  }
}
    

Console Output:

Engine started.

Method Overloading

Concept of Overloading:

Method overloading allows multiple methods with the same name but different parameters in a class. It increases the readability of the program.

Benefits:

Overloading helps in defining the same operation for different data types.


class MathOperations {
  int add(int a, int b) {
    return a + b;
  }

  double add(double a, double b) {
    return a + b;
  }

  public static void main(String[] args) {
    MathOperations math = new MathOperations();
    System.out.println(math.add(5, 3));
    System.out.println(math.add(5.5, 3.3));
  }
}
    

Console Output:

8

8.8

Static Methods

Understanding Static Methods:

Static methods belong to the class rather than any object instance and can be called without creating an instance of the class.

Usage:

They are used to perform operations that don't require any data from object instances.


class Utility {
  static void printMessage() {
    System.out.println("Hello, World!");
  }

  public static void main(String[] args) {
    Utility.printMessage();
  }
}
    

Console Output:

Hello, World!

Return Values from Methods

Returning Values:

Methods can return values using the return statement. The return type must match the method's declared return type.

Example:

The following method returns the square of a number.


class SquareCalculator {
  int square(int x) {
    return x * x;
  }

  public static void main(String[] args) {
    SquareCalculator calculator = new SquareCalculator();
    System.out.println(calculator.square(4));
  }
}
    

Console Output:

16

Void Methods

Understanding Void Methods:

Void methods do not return any value. They perform actions but do not give back any result to the caller.

Example:

The following method prints a message without returning any value.


class Printer {
  void printMessage() {
    System.out.println("This is a void method.");
  }

  public static void main(String[] args) {
    Printer printer = new Printer();
    printer.printMessage();
  }
}
    

Console Output:

This is a void method.

Parameter Passing

How Parameters Work:

Parameters allow data to be passed into methods. They act as variables inside the method body.

Example:

The following method takes two parameters and prints their sum.


class SumCalculator {
  void printSum(int a, int b) {
    System.out.println("Sum: " + (a + b));
  }

  public static void main(String[] args) {
    SumCalculator calculator = new SumCalculator();
    calculator.printSum(10, 20);
  }
}
    

Console Output:

Sum: 30

Recursive Methods

Understanding Recursion:

Recursive methods call themselves to solve a problem. They must have a base case to terminate recursion.

Example:

The following method calculates the factorial of a number using recursion.


class FactorialCalculator {
  int factorial(int n) {
    if (n == 0) return 1;
    else return n * factorial(n - 1);
  }

  public static void main(String[] args) {
    FactorialCalculator calculator = new FactorialCalculator();
    System.out.println(calculator.factorial(5));
  }
}
    

Console Output:

120

Method Visibility

Visibility Modifiers:

Java provides visibility modifiers such as public, private, and protected to control access to methods.

Example:

The following example uses a private method.


class Example {
  private void display() {
    System.out.println("This is a private method.");
  }

  public static void main(String[] args) {
    Example ex = new Example();
    ex.display();
  }
}
    

Console Output:

This is a private method.

Abstract Methods

Understanding Abstract Methods:

Abstract methods are declared without an implementation and must be implemented by subclasses. They are defined in abstract classes.

Example:

The following example shows an abstract method in use.


abstract class Animal {
  abstract void sound();
}

class Dog extends Animal {
  void sound() {
    System.out.println("Woof");
  }

  public static void main(String[] args) {
    Dog dog = new Dog();
    dog.sound();
  }
}
    

Console Output:

Woof

Final Methods

Understanding Final Methods:

Final methods cannot be overridden by subclasses. This is useful for preventing modification of the method's behavior.

Example:

The following example shows a final method in use.


class Base {
  final void show() {
    System.out.println("This is a final method.");
  }
}

class Derived extends Base {
  // Attempting to override the final method will result in a compile-time error
}

public class Main {
  public static void main(String[] args) {
    Derived obj = new Derived();
    obj.show();
  }
}
    

Console Output:

This is a final method.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025