Last Updated:

Access Modifiers in Java

Object-oriented programming allows you to repeat the structure of real objects - their internal and external structure, functionality. For example, let's take a smartphone. It has features that are available to humans (interface, on, off, etc.).

There are functions that are not available to humans – electronic processes, communication, data transmission – they are hidden from access, but are controlled through the interface. To transfer data, a person cannot open the case and do something with the board - you need to press the interface button and the function closed to the user's hands will start.

Access Modifiers in Java

What are access modifiers and why are they needed?

 

Access modifiers are special reserved words that limit the scope of fields, constants, methods, and classes. Access modifiers allow you to restrict user access to individual fields and methods of an object. In programming, this is also called encapsulation.

What are the access modifiers?

 

Access modifiers are internal and external. Internal ones are used inside the class for methods, constants, and fields. External modifiers are used for the class itself.

External modifiers:

  • public;
  • abstract;
  • finall.

Internal modifiers:

  • public;
  • private;
  • protected;
  • static.

Now let's consider them in more detail.

External modifiers

Each external modifier restricts or extends the capabilities of a class:

Public. The class is accessible from anywhere. A class can inherit its functionality from other classes. If a class is declared without this modifier, it is available only in the package where it is declared. For example, this is the main file of the program with the main method.

Abstract. Classes with this modifier describe only field names, method names, and signatures. Method functionality and field values are implemented only in inherited classes. Such a class cannot create objects and does not have a constructor. It only provides basic functionality for the rest of the classes. In this case, the fields can have different values, and the method operators for each class are individual.

Finall. This modifier prevents a class from inheriting functionality from other classes. This class is individual and does not extend its fields and methods to heirs. Attempting to inherit from the finall class will result in a compilation error.

Example:

class Hello{} // A class without a modifier is only available within a package. Usually declared in a file with the function main
public class Hello_public{} // class that is available from everywhere
public abstract class Hello_abstract{} // class that inherits its methods and fields to other classes. Its methods are only defined, but have no operators. It does not have a constructor and cannot create objects.
public finall class Hello_final{} // class that cannot inherit.

Internal modifiers

Each modifier limits the scope of a field or method:

  • Public. The method can be called by other methods of the class and is also available in external code.
  • Private. A method can only be called by other methods of the class, not directly through an object.
  • Protected. The method or field is available only in the current package. Called by objects of classes declared only in the current package. CANNOT be called by class objects from imported packages.
  • Default. A method or field without a modifier. Such methods and fields can be used in the current package.
  • Static. The field or method is called through the class name instead of through the object.

Example:

class Example{
public int a = 1;
private int b = 2;
protected int c = 3;
int d = 4;
static int e = 5;

public Example(){} // empty constructor

public void show(x){ // method for displaying
fields System.out.print(x);
add(); // private method can only be called inside another method or in the constructor

}

private void add(){
a++;

}

static void message(){
System.out.print(«Static»);

}
}

Example obj = new Example();
System.out.print(obj.a); 1
System.out.print(obj.b); error — the private field cannot be used through the System.out.print(obj.c) object
; 3
System.out.print(obj.d); 4
System.out.print(obj.e); System.out.print
error(Example.e) 5

obj.show(b) methods
2 — protected field is available inside the methods of the System.out.print(obj.a) object
; 2 – the previous private method was called and a increased by 1

Example.message(); static — call a static method

Static Initializer

In classes other than fields and methods, you can declare a static initializer. It executes the function of the method, but runs automatically before calling the very first constructor in the class.

Example:

class Cherry{

private int name;
static int sort;

static{
sort = red_cherry;
System.out.println(«Red_cherry initialized»);
}
Cherry(){
name = «Cherry»;
System.out.println(«Constructor»);

}

Cherry item = new Cherry();
// the console will display:
Red_cherry initialized
Constructor

It turns out that when the constructor is called, the initializer is called first, and then the constructor. Subsequent calls to the constructor will not call the initializer and will retain the current value of the static field.

Total

 

Access modifiers, both internal and external, are designed to organize the functionality of objects. Private functions are not available for external management, public functions implement the external interface and can call private functions. Such an organization of classes and objects allows you to approach the creation of real-world objects as closely as possible.