Java Modifiers

Modifiers are the keywords that are used in java for definition of variables, classes or methods.

What are the access modifiers?

public, protected and private are the three access modifiers used in Java.

public – Can be accessed anywhere in classes from same package or in other packages.
private – Can be accessed only within the same class.

protected – Can be accessed within the same class and its subclasses. By default, it can be accessed anywhere from the same package.

What are the non access modifiers?

final, static, abstract, synchronized, strictfp, native, transient and volatile are the eight non access modifiers used in java.

final – The final keyword is used in definition of variables, classes & methods. Final variables are used in situations where it cannot be changed further in code. If it is changed, the compiler will raise compilation error if the final variables are re-initialized. A final class cannot be subclassed. A final method cannot be overriden.

static – The static keyword can also applied to variables, classes & methods. If a variable is defined as static, then the value of variable will be same for every object. If the initial value of the static variable is updated somewhere in the code, then the updated value will remain till the end of the program in the Java Heap Memory for every object unless it is updated again. Always it retains with the latest updated value in it.

If a method is defined static, then creating object for the class is not needed to invoke the method. Rather we can use the Classname.method() to call it.

abstract – The abstract keyword is used while declaring class and method.

  • If a class is declared as abstract, then it cannot be instantiated. It needs to be extended by another subclass.
  • The abstract class can have both abstract and non abstract methods.
  • If a method is declared abstract, then there should not be any implementation. Only the subclass which extends that abstract method’s class will implement it.
  • The subclass must implement all the abstract methods present in the abstract class.

synchronized – If a method is declared with synchronized keyword then, that method can be accessed by only one thread.

transient – During the serialization(We will see this in later chapters) process, the variables marked with transient keyword are skipped.

volatile – The volatile keyword is used in variables where the threads do not use cache value but use from the main memory instead.

strictfp – The strictfp keyword is used to get same result in performing floating point calculations across platforms.

native  – If a method is marked with Native keyword then, it will be implemented in other programming languages such as C. Java Native Interface(JNI) is used to integrate Java code with C.

Posted on July 1, 2014 in Java for Beginners

Share the Story

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top