Methods in Java: Explain method with example

Methods in Java

Methods in Java: methods are blocks of code that perform a specific task and are associated with objects or classes. They are fundamental to object-oriented programming, allowing for code reuse, modularity, and better organization. Here are the various types of methods in Java:

Instance Methods in Java

Instance methods are associated with an instance of a class. They can access instance variables and call other instance methods. To invoke an instance method, you need to create an object of the class.

class Example {
    int number;

    void setNumber(int num) {
        this.number = num;
    }

    int getNumber() {
        return this.number;
    }
}

public class Main {
    public static void main(String[] args) {
        Example obj = new Example();
        obj.setNumber(5);
        System.out.println(obj.getNumber()); // Output: 5
    }
}

Static Methods in Java

Static methods belong to the class rather than any instance. They can be called without creating an object of the class. Static methods can access static variables and other static methods directly but cannot access instance variables or instance methods directly.

class Example {
    static int number;

    static void setNumber(int num) {
        number = num;
    }

    static int getNumber() {
        return number;
    }
}

public class Main {
    public static void main(String[] args) {
        Example.setNumber(5);
        System.out.println(Example.getNumber()); // Output: 5
    }
}

Abstract Methods in Java

Abstract methods are declared without an implementation (i.e., without a body) in an abstract class. Subclasses must override these methods and provide their implementations.

abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape shape = new Circle();
        shape.draw(); // Output: Drawing Circle
    }
}

Final Methods in Java

Final methods cannot be overridden by subclasses. Declaring a method as final ensures that the implementation remains unchanged and secure from being altered.

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

class Derived extends Example {
    // Cannot override display() method here
}

public class Main {
    public static void main(String[] args) {
        Derived obj = new Derived();
        obj.display(); // Output: This is a final method.
    }
}

Synchronized Methods in Java

Synchronized methods are used to control access to a method by multiple threads. When a thread invokes a synchronized method, it acquires a lock on the object and releases it when the method completes.

class Example {
    synchronized void printNumbers() {
        for (int i = 1; i <= 5; i++) {
            System.out.println(i);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Example obj = new Example();
        Thread t1 = new Thread(() -> obj.printNumbers());
        Thread t2 = new Thread(() -> obj.printNumbers());

        t1.start();
        t2.start();
    }
}

Native Methods in Java

Native methods are implemented in a language other than Java, typically C or C++. These methods are declared using the native keyword and linked via the Java Native Interface (JNI).

class Example {
    native void nativeMethod();

    static {
        System.loadLibrary("ExampleLibrary"); // Load the native library
    }
}

public class Main {
    public static void main(String[] args) {
        Example obj = new Example();
        obj.nativeMethod(); // Call to native method
    }
}

Constructor Methods in Java

Constructors are special methods called when an object is instantiated. They initialize the new object and set initial values for its fields.

class Example {
    int number;

    Example(int num) {
        this.number = num;
    }

    int getNumber() {
        return this.number;
    }
}

public class Main {
    public static void main(String[] args) {
        Example obj = new Example(5);
        System.out.println(obj.getNumber()); // Output: 5
    }
}

Understanding these different types of methods in Java is crucial for effective object-oriented programming and designing robust, maintainable code.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *