Skip to content

Java class modifiers

How are classes build in java?

There are a lot of classes with a lot of parameters

A class always begins with either the public or default or abstract access modifier. Then comes the class name

Example:

1
2
3
public class MyClass {
    //methods and atributes
}
Then in a class you can have Methods or Atributes where the actual code is.
flowchart LR
0[~] --> a([Access Modifiers]) --> b([class]) --> c([Class name])
0 --> b

Constructor

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

The constructor Method is the first method that is called when a class is created. It has the same name as the class and no return type. It is automaticly called when a class is created.

Methods

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

Methods are build like this

flowchart LR
0([~]) --> A([Access Modifiers]) --> B([Method Modifier]) --> C([Return Type]) --> D([Method Name]) --> E([Parameters]) --> F([Method Body])
A --> C
0 --> C
D --> F

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class MyClass {
    // Method
  static void myMethod() {
    // print Hello World!
    System.out.println("Hello World!");
  }
  // Main method
  //access modifier, Method Modifier, return type, method name, parameters, method body
  public static void main(String[] args) {
    // Call the method
    myMethod();
  }
}

Atributes

Attributes are variables within a class. When an object is created from a class, the object will have these attributes.

Example:

1
2
3
public class MyClass {
  int x = 5;
}

Classes

Class modifiers

Non-access modifiers

Modifier Description
final The class cannot be inherited by other classes (You will learn more about inheritance in the Inheritance chapter)
abstract The class cannot be used to create objects (To access an abstract class, it must be inherited from another class. You will learn more about inheritance and abstraction in the Inheritance and Abstraction chapters)

Access modifiers

Modifier Description
public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package. This is used when you don't specify a modifier. You will learn more about packages in the Packages chapter
protected The code is accessible in the same package and subclasses. You will learn more about subclasses and superclasses in the Inheritance chapter

Methods and attributes

non access modifiers

Modifier Description
final Attributes and methods cannot be overridden/modified
static Attributes and methods belongs to the class, rather than an object. You don't really wanna use this one.
abstract Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for example abstract void run();. The body is provided by the subclass (inherited from). You will learn more about inheritance and abstraction in the Inheritance and Abstraction chapters
transient Attributes and methods are skipped when serializing the object containing them
synchronized Methods can only be accessed by one thread at a time
volatile The value of an attribute is not cached thread-locally, and is always read from the "main memory"

Access modifiers

Modifier Description
public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package. This is used when you don't specify a modifier. You will learn more about packages in the Packages chapter
protected The code is accessible in the same package and subclasses. You will learn more about subclasses and superclasses in the Inheritance chapter

Sources

  • https://www.w3schools.com/java/java_modifiers.asp explanation of modifiers
  • https://en.wikipedia.org/wiki/Syntax_diagram For syntax diagram