Java methods examples

Ivy 300 Published: 06/26/2024

Java methods examples

Here are some examples of Java method:

Method Declaration

A method is a block of code that can be invoked repeatedly from different parts of your program, and which takes arguments and returns values. Here's an example of how to declare a method in Java:

public class HelloWorld {

public static void main(String[] args) {

greet("John");

}

public static void greet(String name) {

System.out.println("Hello " + name + "!");

}

}

In this example, greet is a method that takes one argument of type String, and prints out a greeting message to the console.

Method Invocation

To call or invoke a method, you simply use its name followed by parentheses containing any necessary arguments:

HelloWorld hw = new HelloWorld();

hw.greet("Jane");

In this example, we create an instance of the HelloWorld class and then invoke the greet method on it, passing in the argument "Jane".

Method Overload

Java allows you to define multiple methods with the same name but different parameters. This is called method overload:

public class Calculator {

public static int add(int a, int b) {

return a + b;

}

public static double add(double a, double b) {

return a + b;

}

}

In this example, we have two methods add that take different types of parameters. We can call them like this:

int sum = Calculator.add(2, 3);

double total = Calculator.add(5.5, 4.5);

Method Overriding

Java also allows you to define a method in a subclass that has the same name and signature as a method in its superclass. This is called method overriding:

public class Animal {

public void sound() {

System.out.println("The animal makes a sound");

}

}

public class Dog extends Animal {

@Override

public void sound() {

System.out.println("The dog barks");

}

}

In this example, the Dog class overrides the sound method of its superclass Animal. When we create an instance of Dog and call sound, it will print out "The dog barks":

Dog myDog = new Dog();

myDog.sound(); // prints out "The dog barks"

These are just a few examples of how you can use Java methods. Methods are a powerful feature in Java that allow you to encapsulate code and reuse it throughout your program.

Here are some other method examples:

Getter and Setter methods: These are methods that return or set the value of a field:
public class Person {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

Mathematical methods: These are methods that perform mathematical operations:
public class MathUtils {

public static int add(int a, int b) {

return a + b;

}

public static double multiply(double a, double b) {

return a * b;

}

}

String manipulation methods: These are methods that manipulate strings:
public class StringUtils {

public static String reverse(String str) {

StringBuilder reversed = new StringBuilder();

for (int i = str.length() - 1; i >= 0; i--) {

reversed.append(str.charAt(i));

}

return reversed.toString();

}

public static String toUpperCase(String str) {

return str.toUpperCase();

}

}

These are just a few examples of the many types of methods you can write in Java. Methods are a fundamental part of any Java program and allow you to encapsulate code and reuse it throughout your program.

How to call a method in Java?

I'm happy to help you with that!

In Java, there are several ways to call a method, depending on the context and the type of method. Here's a comprehensive overview:

1. Instance Method: An instance method is a non-static method that belongs to an object or an instance of a class. To call an instance method, you need to create an object of that class and then call the method on that object.

Example:

public class Dog {

public void bark() {

System.out.println("Woof!");

}

}

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.bark(); // Output: Woof!

}

}

In this example, the bark() method is an instance method of the Dog class. We create an object myDog of the Dog class and then call the bark() method on that object.

2. Static Method: A static method belongs to a class itself, rather than instances of the class. You can call a static method without creating an instance of the class.

Example:

public class MathUtils {

public static int add(int x, int y) {

return x + y;

}

}

public class Main {

public static void main(String[] args) {

int result = MathUtils.add(2, 3); // Output: 5

}

}

In this example, the add() method is a static method of the MathUtils class. We can call the add() method directly on the MathUtils class without creating an instance.

3. Abstract Method: An abstract method is declared in an abstract class and must be implemented by any subclass that extends the abstract class.

Example:

public abstract class Animal {

public abstract void makeSound();

}

public class Dog extends Animal {

public void makeSound() {

System.out.println("Woof!");

}

}

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.makeSound(); // Output: Woof!

}

}

In this example, the makeSound() method is an abstract method declared in the Animal class. The Dog class extends the Animal class and implements the makeSound() method.

4. Interface Method: An interface method is a method that must be implemented by any class that implements that interface.

Example:

public interface Printable {

void print();

}

public class Document implements Printable {

public void print() {

System.out.println("Printing document...");

}

}

public class Main {

public static void main(String[] args) {

Document myDoc = new Document();

myDoc.print(); // Output: Printing document...

}

}

In this example, the print() method is an interface method declared in the Printable interface. The Document class implements the Printable interface and implements the print() method.

5. Overloaded Method: An overloaded method is a method with the same name as another method but with different parameters or return types.

Example:

public class Calculator {

public int add(int x, int y) {

return x + y;

}

public double add(double x, double y) {

return x + y;

}

}

public class Main {

public static void main(String[] args) {

Calculator calculator = new Calculator();

int result1 = calculator.add(2, 3); // Output: 5

double result2 = calculator.add(2.0, 3.0); // Output: 5.0

}

}

In this example, the add() method is overloaded with different parameters and return types.

6. Override Method: An override method is a method that provides a specific implementation for a class when it overrides a method from its superclass or interface.

Example:

public abstract class Animal {

public void sound() {

System.out.println(" generic sound");

}

}

public class Dog extends Animal {

@Override

public void sound() {

System.out.println("Woof!");

}

}

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.sound(); // Output: Woof!

}

}

In this example, the sound() method is overridden in the Dog class to provide a specific implementation.

In summary, there are several ways to call a method in Java, including instance methods, static methods, abstract methods, interface methods, overloaded methods, and override methods. Understanding these different types of methods can help you write more efficient and effective code.