Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Java Modifiers


Java Modifiers

In Java, modifiers are keywords used to control access, visibility, and behavior of classes, methods, and variables. Java provides several types of modifiers, including access modifiers, non-access modifiers, and other specific modifiers. Here's an overview of Java modifiers:

Access Modifiers:

Access modifiers control the accessibility of classes, methods, and variables in Java. There are four types of access modifiers:

  1. public: Accessible from any other class.
  2. protected: Accessible within the same package and by subclasses (even if they are in a different package).
  3. default (no modifier): Accessible only within the same package.
  4. private: Accessible only within the same class.

Example:


    public class MyClass {
        public int publicVariable;
        protected int protectedVariable;
        int defaultVariable; // default access
        private int privateVariable;
    }
   

Non-Access Modifiers:

Non-access modifiers change the behavior of classes, methods, and variables in various ways. Some common non-access modifiers include:

  1. static: Belongs to the class rather than any specific instance. Can be accessed without creating an instance of the class.
  2. final: Indicates that a variable's value cannot be changed, a method cannot be overridden, or a class cannot be subclassed.
  3. abstract: Indicates that a class or method is incomplete and must be implemented by subclasses (abstract classes or methods cannot be instantiated).
  4. synchronized: Ensures that only one thread can access a method or block of code at a time.
  5. volatile: Indicates that a variable's value may be changed unexpectedly by other parts of the program.
  6. transient: Specifies that a variable should not be serialized when an object is written to persistent storage.
  7. native: Indicates that a method is implemented in native code (not written in Java).

Example:


    public class MyClass {
        static int staticVariable;
        final int finalVariable;
        abstract void abstractMethod();
        synchronized void synchronizedMethod() { }
        volatile int volatileVariable;
        transient int transientVariable;
        native void nativeMethod();
   

Specific Modifiers:

Java also provides specific modifiers for particular use cases, such as:

Summary:

Modifiers in Java control the access, visibility, and behavior of classes, methods, and variables. Access modifiers determine the accessibility of classes, methods, and variables. Non-access modifiers change the behavior of classes, methods, and variables in various ways, such as static, final, abstract, synchronized, volatile, transient, and native. Java also provides specific modifiers for particular use cases, such as strictfp.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java