• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Java Field and Method Modifiers

by admin

In this post, you will learn to:

  • Define field and method modifiers.
  • State the use of volatile modifiers.
  • Explain the use of native modifiers.
  • Describe the transient modifier and its use.
  • List the rules for using access control specifiers.

Field and Method Modifiers

Field and method modifiers are keywords used to identify fields and methods that need to be declared for controlling access to users. Some of these can be used in conjunction with access specifiers, such as public and protected. Java provides the volatile, transient, and native keywords that act as field and method modifiers.

‘volatile’ Modifier

The volatile modifier allows the content of a variable to be synchronized across all running threads.Therefore, when the value of the variable changes or is updated, all threads will reflect the same change. The volatile modifier is applied only to fields. Constructors, methods, classes, and interfaces cannot have this modifier. This modifier is not frequently used. volatile variables are useful in multiprocessor environments and are not frequently used otherwise.

‘native’ Modifier

The native modifier is used only with methods and indicates that the implementation of the method is in a language, other than in Java. Constructors, fields, classes, and interfaces cannot have this modifier. The methods declared using the native modifier are called native methods. The Java source file typically contains only the declaration of the native method and not its implementation.

Native methods violate Java’s platform independence feature, and hence must not be used, unless absolutely required. The callers of native methods need not even know that the method was declared in the class, because they invoke the method normally, just as other methods are invoked. Some classes in the Java API have native methods. The Object class, for instance, present in the java.lang package, has many native methods.

‘transient’ Modifier

The transient modifier is used to declare fields that are not saved or restored as a part of the state of the object. Often, serialization proves to be quite expensive. In such scenarios, the transient keyword reduces the amount of data being serialized, improves performance and reduces costs.

Note: Serialization in Java converts an object’s internal state into a binary stream of bytes. The stream can be written to a disk or stored in memory.

Rules for Using Field Modifiers

Some of the rules for using field modifiers are as follows:

  • final fields cannot be volatile.
  • native methods in Java cannot have a body.
  • Declaring a transient field as static or final should be avoided as far as possible.
  • native methods violate Java’s platform independence characteristic. Therefore, they should not be used frequently.
Introduction to Java Packages
Java Access Modifiers

Filed Under: Java

Some more articles you might also be interested in …

  1. Java Classes and Objects
  2. Java – Arrays
  3. Java Basics: Overriding Methods, Polymorphism, and Static Classes
  4. Basic Java: Generics and Collections
  5. Basics of Java Data Types
  6. Basics of Java Operators
  7. Arrays in Java
  8. Nested Class in Java
  9. Java if and switch-case Statements
  10. Java Polymorphism

You May Also Like

Primary Sidebar

Recent Posts

  • vgextend Command Examples in Linux
  • setpci command – configure PCI device
  • db_load command – generate db database
  • bsdtar command – Read and write tape archive files

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright