Witscale Test Center

3.5 Method declaration and modifiers


3.5 Method declaration and modifiers

Like classes and variables, methods too have can have the access modifiers and other (non-access) modifiers. Since methods deal with implementation, the implementation specific modifiers are applicable to them, For example, modifiers static, final, native, abstract and synchronized are applicable to methods. They are called implementation-specific modifiers because they change the way a method is implemented. The storage-specific modifiers, transient and volatile are not applicable to methods. Following general format for method declaration might help you in remembering the applicable method modifiers:

 

[Access Modifiers] [static] [final] [native] [abstract] [synchronized] returnType methodName ([method Parameters]) [throws Exceptions]

 

The items between [ ] are optional. Let us quickly summarize what each modifier indicates.

         The Access Modifier  defines the accessibility level (see section 3.2) of the method

         static modifier indicates  that the method is a class method as opposed to an instance method.

         final modifier indicates  that the method cannot be overridden.

         native modifier indicates that the method is only defined here and implementation is linked to a native library.

         abstract modifier indicates that the method is only defined here. The implementation is provided in subclasses.

         synchronized modifier indicates that the method can only be accessed by one thread at a time.

Each of these modifiers are optional. Let us discuss each one in detail.