Various Permitted Modifiers in JAVA

Harsh Gupta - Tech Writer
Modifiers determine or define how the data members and methods are used in other classes and objects. The main difference between access specifiers and modifiers is that access specifiers define the accessibility of the data members in a class and the modifiers determine how these methods are used and modified by other classes. The various modifiers permitted in Java are:

static

final

abstract

native

synchronized

static Modifier

The static keyword is used with methods, variables, and inner classes. The static keyword is used to define class variables and methods that belong to a class and not to any particular instance of the class.

A static method associates the data members with a class and not the objects of the class. Therefore, all the objects of a class share the same static data members and methods. Non-static methods are those, which are associated with objects, and as a result the values of the data members differ for different objects. You cannot access non-static data members and methods from a static method.

For example, in an online shopping application, you can keep track of the number of books sold by keeping a static counter data member in the Books class that increment each time a book is sold.

final

The final keyword is used with methods, variables, and classes. The final modifier indicates that the data member cannot be modified. For example, consider a variable that has been assigned a value. If the variable has been declared final, you cannot modify the value of the variable and if you try to do so, It will cause run-time error. A variable declared final Is Initialized at the time It la declared.

The final modifier does not allow the class to be inherited. It is used to create classes that serve as a standard, and you do not want anybody to modify the methods in a subclass and use them in a different manner. The final modifier has the following characteristics:

A final method cannot be modified in the subclass.

A final class can never be inherited.

All the methods and data members in a final class are implicitly final.

Note: A class can be declared as final If you do not want the class to be subclassed.

abstract

The abstract keyword is used to declare classes that only define common properties and behavior of other classes. An abstract class is used as a base class to derive specific classes of the same type, for example, you can create an abstract Books class that contains the common data members, such as title, page numbers, and type of binding for all the books.

native

The native modifier is used only with methods. It is used to inform the compiler that the method has been coded in a programming language other than Java, such as C or C++. The native keyword with a method indicates that the method lies outside the Java Runtime Environment (JR£).

The following syntax shows how to declare a native modifier:

public native void nativeMethod (value1, value2, .....);

Note: The native method makes a program platform dependent. In addition, writing native methods must be avoided. They are used when you have an existing code in another language and do not want to rewrite the code in Java.

synchronized

The synchronized modifier is used for methods. The synchronized modifier controls the access to a block of code in a multithreaded programming environment. A thread is a unit of execution within a process. Java supports multithreaded programming and each thread defines a separate path of execution.

In a multithreaded program, you need to synchronize various threads. As a result of synchronized tag, only one thread can access a shared resource when two or more threads need access to the resource at the same time- For example, if multiple threads need to print a document, only one thread can access the printer as the result of synchronization.

Published by Harsh Gupta - Tech Writer

I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com  View profile

  • A class can be declared as final If you do not want the class to be subclassed
  • The final modifier does not allow the class to be inherited
The main difference between access specifiers and modifiers is that access specifiers define the accessibility of the data members in a class and the modifiers determine how these methods are used and modified by other classes.

To comment, please sign in to your Yahoo! account, or sign up for a new account.