Declaring Methods  «Prev  Next»


Lesson 8Other modifiers
ObjectiveIntroduce and explain the other modifiers supported by Java.

Java - Non Access Modifiers

  • abstract: The abstract modifier identifies abstract classes and methods. Abstract classes declare abstract methods and defer the implementation of these methods to their subclasses. When an abstract method is declared, its body is replaced by a semicolon.
  • Final: The final modifier is used with classes, field variables, local variables, methods, and method parameters. It indicates that an item may not be changed. A final class may not be subclassed. A final variable or parameter may not have its value changed (once it has been initialized). A final method may not be overridden.
  • Native:
    The native modifier identifies a native method. Native methods are written in languages other than Java and executed outside the Java virtual machine. The body of a native method is replaced by a semicolon when it is declared. Native methods may not be abstract.
  • Static: The static modifier identifies a variable or method that applies to a class as a whole rather than to specific instances of a class. Static variables are shared by all class instances. Static methods are invoked on the class in which they are declared, as well as on instances of a class. Static inner classes are covered in Module 3.


Invoking static methods in Java

The class name in which a static method is declared may be used in the method invocation. For example, if static method1() is declared in MyClass, then method1() may be invoked as MyClass.method1().
  • Class Methods The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in
    ClassName.methodName(args)
    

    Note: You can also refer to static methods with an object reference as in instanceName.methodName(args) but this is discouraged because it does not make it clear that they are class methods. A common use for static methods is to access static fields. For example, we could add a static method to the Bicycle class to access the numberOfBicycles static field:
    public static int getNumberOfBicycles() {
        return numberOfBicycles;
    }
    

Not all combinations of instance and class variables and methods are allowed:
  1. Instance methods can access instance variables and instance methods directly.
  2. Instance methods can access class variables and class methods directly.
  3. Class methods can access class variables and class methods directly.
  4. Class methods cannot access instance variables or instance methods directly.
  5. Class methods must use an object reference.
Also, class methods cannot use the this keyword as there is no instance for this to refer to.


1) synchronized, 2) transient, and 3) volatile in Java

In Java, the keywords synchronized, transient, and volatile are classified as modifiers. These are used to modify the behavior of variables or methods in specific ways:
  1. synchronized: It is a method or block modifier that ensures that a method or block of code can only be accessed by one thread at a time.
    This is useful for managing concurrent access to shared resources.
    The synchronized modifier identifies synchronized methods and synchronized statements.
    Example:
    public synchronized void methodName() {
    // Code that is thread-safe
    }
    
  2. transient: It is a variable modifier that indicates that a variable should not be serialized when the object containing it is serialized. In other words, the value of a transient variable is not persisted during serialization. The transient modifier identifies a variable that may not be serialized. A transient variable may not be declared as final or static.
    Example:
    private transient int tempData; // This field will not be serialized
    
  3. volatile:It is a variable modifier used for multi-threaded programming. It indicates that a variable's value may be changed by multiple threads, ensuring that the value of the variable is always read from the main memory, not from a thread's local cache.
    The volatile modifier is used to indicate that a variable may be modified asynchronously in a multiprocessor environment.
    Example:
    private volatile boolean isActive; // This ensures visibility across threads
    

These modifiers are mainly used to manage synchronization, serialization, and visibility in multi-threaded environments.

SEMrush Software