Access Modifiers Java Meaning Functionality

Access Modifiers Java Meaning Functionality

We know access specifiers specify the access and access modifiers modify the access of variables, methods and classes. In general, an access modifier works between the classes of the same application or within the classes of same package.

Note: I have seen some people calling access modifiers also as specifiers. It seems for them Java does not have access modifiers.

To understand better Access Modifiers Java, let us take the examples of static and final access modifiers. Generally a method or variable is called with an object, but declaring static, you do not require the object. Similarly is with final modifier also. Declaring a method final, the subclass is not allowed to override the super method and a final variable cannot be reassigned. Synchronized gives a meaning of multiple threads cannot access the same method at a time. Now, you understand the meaning of "modifying the access".

The access modifiers supported by Java are static, final, abstract, synchronized, native, volatile, transient and strictfp. Many are discussed at appropriate places in very detail and a few are explained hereunder.

Access Modifiers Java: 1. native

"native" keyword applies to methods. The code of a native method is written in some other language like C or C++. At execution time, this native code is executed separately and put with the Java output and is given. To do all this, Java includes Java Native Interface (JNI) which is inbuilt into JDK. “native” permits the Java developer to write the machine-dependent code in other language (like C/C++) and make use in Java. Alternatively, if a C or C++ function exists with a very complex code, it is not required to convert it into Java code and instead can be used directly in a Java program.

Observe some methods of Java API.

Observe native keyword in the above two statements. The functions' code is written in other language (means, not in Java). To execute these methods, Java takes the help of underlying OS.

Access Modifiers Java: 2. volatile

The keyword volatile applies to variables and objects. A volatile variable value is more likely to get changed in the code. A volatile variable is treated differently by the JVM.

Volatile variables are not optimized (to minimize execution time) for performance by the compiler as their values are expected to get changed at any time without information. Volatile is better used with multiple threads that can change a variable value often.

Volatile can be used as follows.

Volatile does not have any meaning when applied to final variables or immutable objects or synchronized block.

The following two statements give compilation error.

final volatile int x = 10;
volatile final int x = 10;

Access Modifiers Java: 3. transient Modifier

It is used with Serialization. "transient" is a keyword which means the data is not serialized. In object serialization, the objects along with the data are serialized. If the data is not required to be serialized and thereby not written to a file (or sent to the remote client/server), then declare the data as transient.

Following is the way how to declare a variable as transient.

A program on transient keyword is available at Java Serialization Example.

Access Modifiers Java: 4. strictfp

A floating-point value, in a language, is platform-dependent. That is, the same floating value, in a Java program, executed on different operating systems may give different outputs (precision). To get the same precision (regardless of hardware, software and OS) on every operating system, declare the class or method as strictfp. "strictfp" is a keyword added in JDK 1.2 version.

"strictfp" is abbreviation for "strict floating-point".

If a class is strictfp, all the code in the class is evaluated with the strict floating point precision as laid in IEEE 754 standards. strictfp follows the rules formatted by IEEE 754. JVM does not apply its own precision.

IEEE and IEEE 754

IEEE is an abbreviation for Institute of Electrical and Electronics Engineers. It is a service-oriented body managed by professional engineers. It is started in 1963 and operated in 150 countries with over 3.5 lakh members. It is an amalgamated body of Institute of Radio Engineers and American Institute of Electrical Engineers. It sets standards for many like for floating-point values used by CPU.

Following table gives the list of access specifiers and modifiers Java that can be applied to variables, methods and classes.

specifier/modifier local variable instance variable method class
public NA A A A
protected NA A A NA
default A A A A
private NA A A NA
final A A A A
static NA A A NA
synchronized NA NA A NA
native NA NA A NA
volatile NA A NA NA
transient NA A NA NA
strictfp NA NA A A

A: Allowed NA: Not Allowed

B. Topics generally confusing, get clear understanding.

1. Can a constructor be static?
2. Can a constructor be private?
3. Java Constructor Properties – Synchronized
4. Can a abstract class have constructor? Can interface be declared as abstract?
5. Can a abstract method be static?
6. Can abstract class have main()?
7. Does constructor really returns something?

4 thoughts on “Access Modifiers Java Meaning Functionality

    1. S. Nageswara Rao, Corporate Trainer Post author

      Not possible. Static block is also like a method (containing some statements within braces) but with special properties. We cannot define a method inside a method. For this reason, method cannot be written inside a static block.

      A local variable cannot be static. For this reason, a variable inside a static block cannot be static.

Leave a Reply

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