|
Do you need help with your Java programming?
Click here for instant help with your Java code. |
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! | |
Search for code examples on this site
