본문 바로가기
Language/Java

Java access modifiers

by Zayne 2023. 5. 24.

In Java, access modifiers are used to set the accessibility of classes, interfaces, variables, methods, constructors, etc.

There are four types of Java access modifiers

  1. Private : The access level of a private modifier is only within the class.It cannot be accessed from outside the class.
  2. Default : The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
  3. Protected : The access level is within the package and outside the package through child class. If you do not make the child. If you do not make the child class, it is considered to be default.
  4. Public : The access level is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

 

// in the file Main.java
package com.example.main;

import com.example.second.Second;

public class Main {
    public static void main(String[] args) {
        First first = new First();

        System.out.println("Public: " + first.publicVariable);  // accessible
        // System.out.println("Protected: " + first.protectedVariable);
        // not accessible, because it's in a different package and not a subclass
        // System.out.println("Default: " + first.defaultVariable);
        // not accessible, because it's in a different package
        // System.out.println("Private: " + first.privateVariable);
        // not accessible, because it's in a different class

        Second second = new Second();
        second.testAccessFromAnotherPackage();
    }
}

// in the file First.java
package com.example.first;

public class First {
    public int publicVariable = 1;
    protected int protectedVariable = 2;
    int defaultVariable = 3;  // no modifier => default access
    private int privateVariable = 4;

    public void testAccessInSameClass() {
        System.out.println("Public: " + publicVariable);  // accessible
        System.out.println("Protected: " + protectedVariable); // accessible
        System.out.println("Default: " + defaultVariable); // accessible
        System.out.println("Private: " + privateVariable); // accessible
    }
}

// in the file Second.java
package com.example.second;

import com.example.first.First;

public class Second extends First {
    public void testAccessFromAnotherPackage() {
        System.out.println("Public: " + publicVariable);  // accessible
        System.out.println("Protected: " + protectedVariable);
        // accessible, because Second is a subclass of First
        // System.out.println("Default: " + defaultVariable);
        // not accessible, because it's in a different package
        // System.out.println("Private: " + privateVariable);
        // not accessible, because it's in a different class
    }
}

'Language > Java' 카테고리의 다른 글

replace, replaceAll  (0) 2023.05.25
Java Collections Framework  (0) 2023.05.25
Super  (0) 2023.05.11
다형성(Polymorphism)  (0) 2023.05.07
스트림  (0) 2023.03.08