What are the types of methods in java with examples

Enid 107 Published: 11/06/2024

What are the types of methods in java with examples

Java is a popular programming language that offers various methods for coding and programming. Methods are reusable blocks of code that can perform specific tasks, make decisions, or manipulate data. There are several types of methods in Java:

void Method(): This method does not return any value and is used to perform some action. For example:
public class Main {

public static void printHello() {

System.out.println("Hello");

}

public static void main(String[] args) {

printHello();

}

}

In this code, the printHello() method prints "Hello" to the console.

Method That Returns a Value: This type of method returns some value. For example:
public class Main {

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

return (a + b);

}

public static void main(String[] args) {

int result = sum(10, 20);

System.out.println("Sum: " + result);

}

}

In this code, the sum() method returns the sum of two integers. The value is then printed to the console.

Method with Parameters: This type of method accepts some parameters or input values. For example:
public class Main {

public static void printName(String name) {

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

}

public static void main(String[] args) {

String name = "John";

printName(name);

}

}

In this code, the printName() method prints a personalized greeting based on the name input.

Method That Accepts an Array as Parameter: This type of method accepts an array as a parameter and can manipulate it. For example:
public class Main {

public static void printArray(int[] array) {

for (int i = 0; i < array.length; i++) {

System.out.println("Element " + i + ": " + array[i]);

}

}

public static void main(String[] args) {

int[] array = {1, 2, 3};

printArray(array);

}

}

In this code, the printArray() method prints all elements of an integer array.

Method That Throws Exceptions: This type of method can throw exceptions when something goes wrong. For example:
public class Main {

public static void checkAge(int age) throws Exception {

if (age < 18) {

throw new Exception("You are too young");

} else {

System.out.println("You are old enough");

}

}

public static void main(String[] args) {

try {

checkAge(17);

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

In this code, the checkAge() method throws an exception if the age is less than 18. The exception is then caught and printed.

Method That Calls Another Method: This type of method can call another method to perform some action. For example:
public class Main {

public static void printHello() {

System.out.println("Hello");

}

public static void main(String[] args) {

sayHello();

}

public static void sayHello() {

printHello();

}

}

In this code, the sayHello() method calls another method printHello() to perform some action.

Method That Uses Lambda Expressions: This type of method can use lambda expressions for concise coding. For example:
public class Main {

public static void printNumbers(int[] array) {

Arrays.stream(array).forEach(System.out::println);

}

public static void main(String[] args) {

int[] array = {1, 2, 3};

printNumbers(array);

}

}

In this code, the printNumbers() method uses lambda expressions to print all elements of an integer array.

These are some of the common types of methods in Java. Each type has its own unique features and usage scenarios.

What are the types of methods in java w3schools

I'm happy to help you with that! However, I must inform you that W3Schools is a website that primarily focuses on web development and programming languages like HTML, CSS, JavaScript, and PHP, rather than Java specifically.

That being said, here are the different types of methods in Java:

Constructor Methods: These are special methods used to initialize objects. They have the same name as the class and no return type (not even void). A constructor is called when an object is created from a class.

Instance Methods: These are the most common type of method, which can be called on any instance of a class. They typically operate on the state of the object and can modify its properties.

Static Methods: These methods belong to a class rather than an instance. They cannot access or modify the state of objects. Static methods are essentially global functions that belong to a class.

Abstract Methods: These are declared in abstract classes (a class that can't be instantiated) and must be implemented by any non-abstract subclass. Abstract methods have no implementation, just a declaration.

Interface Methods: Interfaces can also declare methods with no implementation. Any class implementing the interface must provide an implementation for these methods.

Override Methods: These are instance methods declared in a subclass that provides a specific implementation for a method that is already defined in its superclass. This allows classes to specialize behavior or provide alternative implementations.

Overload Methods: Also known as compile-time polymorphism, this is when multiple methods with the same name but different parameter lists can be defined in a class. The correct overload to call is determined at compile-time based on the method signature and the arguments passed.

Final Methods: These are instance or static methods that cannot be overridden by subclasses. This helps prevent subclassing and ensures specific behavior is maintained.

In Java, you can also define default methods for interfaces starting from Java 8. Default methods provide a way to add new functionality to an interface without breaking existing code that implements the interface.

These are some of the key types of methods in Java. While W3Schools may not have a dedicated section on Java methods, understanding these concepts is essential for effective Java programming.