Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Modifiers

Modifiers

By now, you’re likely well-acquainted with the public keyword, which is frequently featured in our examples.

public class Main

The public keyword serves as an access modifier, determining the access level for classes, attributes, methods, and constructors.

Modifiers are categorized into two groups:

  • Access Modifiers governing the access level
  • NonAccess Modifiers offering additional functionality without controlling access level

Access Modifiers

Regarding classes, you have the option to use either public or default access modifiers:

Modifier

Description

public

The class can be accessed by any other class.

default

The class is solely accessible from classes within the same package. This is the default behavior when no modifier is specified. Further details on packages will be covered in the Packages chapter.

Attributes, methods, and constructors can be specified using one of the following access modifiers:

Modifier

Description

public

The code can be accessed by all classes.

private

The code is exclusively accessible within the declared class.

default

The code is solely accessible within the same package. This is the default behavior when no modifier is specified. Further details on packages will be covered in the Packages chapter.

protected

The code is accessible within the same package and subclasses. Further details on subclasses and superclasses will be covered in the Inheritance chapter.

Non-Access Modifiers

Regarding classes, you have the option to use either final or abstract modifiers:

Modifier

Description

final

The class cannot be extended by other classes. Further information on inheritance will be provided in the Inheritance chapter.

abstract

The class cannot be instantiated. To utilize an abstract class, it must be extended by another class. Further details on inheritance and abstraction will be covered in the Inheritance and Abstraction chapters.

Access Modifiers governing the access lev

Attributes and methods can be specified using one of the following modifiers:

Modifier

Description

final

Attributes and methods are immutable; they cannot be overridden or modified.

static

Attributes and methods are associated with the class itself, rather than individual objects.

abstract

This modifier is exclusively applicable within an abstract class and is limited to methods. It signifies that the method lacks an implementation, as exemplified by abstract void run();. The implementation is provided by the subclass through inheritance. Further insights on inheritance and abstraction will be provided in the respective chapters.

transient

Attributes and methods marked with this modifier are omitted during the serialization process of the containing object.

synchronized

Methods synchronized restrict access to only one thread at a time.

volatile

The value of a volatile attribute is consistently read from the “main memory” and not cached locally to any thread.

Final

To prevent the overriding of existing attribute values, declare attributes as final :

Example

public class Main {
  final int x = 10;
  final double PI = 3.14;

  public static void main(String[] args) {
    Main myObj = new Main();
    myObj.x = 50; // will generate an error: cannot assign a value to a final variable
    myObj.PI = 25; // will generate an error: cannot assign a value to a final variable
    System.out.println(myObj.x);
  }
}

Static

A static method can be accessed without the need to create an object of the class, unlike public methods :

Example

An example illustrating the distinctions between static and public methods:

public class Main {
  // Static method
  static void myStaticMethod() {
    System.out.println("Static methods can be called without creating objects");
  }

  // Public method
  public void myPublicMethod() {
    System.out.println("Public methods must be called by creating objects");
  }

  // Main method
  public static void main(String[ ] args) {
    myStaticMethod(); // Call the static method
    // myPublicMethod(); This would output an error

    Main myObj = new Main(); // Create an object of Main
    myObj.myPublicMethod(); // Call the public method
  }
}

Abstract

An abstract method is associated with an abstract class and lacks a body. The implementation for the method is provided by the subclass.

Example

// Code from filename: Main.java
// abstract class
abstract class Main {
public String fname = "John"; public int age = 24; public abstract void study(); // abstract method } // Subclass (inherit from Main) class Student extends Main { public int graduationYear = 2018; public void study() { // the body of the abstract method is provided here System.out.println("Studying all day long"); } } // End code from filename: Main.java // Code from filename: Second.java class Second { public static void main(String[] args) { // create an object of the Student class (which inherits attributes and methods from Main) Student myObj = new Student(); System.out.println("Name: " + myObj.fname); System.out.println("Age: " + myObj.age); System.out.println("Graduation Year: " + myObj.graduationYear); myObj.study(); // call abstract method
}
}