Method Modifiers in Java


This page shows a list of the different modifiers that a method can have, as well as examples of how to use them.
A method may contain zero or more modifiers

public, private, protected - These are all access modifiers which determines from where a method can be called. More about access modifiers later in this tutorial.

static - This modifier tells that the method is a class method and belongs to the class rather than an instance of the class.

synchronized - This is used for multithreaded applications. If a method is declared synchronized the thread that executes it must obtain a lock on the object that contains the method before it can be executed. Note that this is the case although a class may just have one method that is declared synchronized. If the method is static the lock must be obtained on the class.

abstract - A method that is declare abstract does not have a method body, the signature just ends with a semicolon. If a class has one or more methods that are declared abstract it must itself be declared abstract and cannot be instantiated.

final - A method declared final cannot be overridden by subclasses.

native - If a method is declared native it specifies that the method implementation is written in some "native" language such as C and is provided externally to the Java program. Just like the abstract modifier a method declared native does not have a method body, the signature ends with a semicolon.

Here are a few examples of modifiers:



    //A public abstract method
    public abstract void methodA();

    //A protected static method
    protected static void methodB() {
        
    }
    
    //A public synchronized final method
    public synchronized final void methodC() {
        
    }

Previous     Next

Tutorial Home


Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: