developer tip

Java의 기본 액세스 수정자는 무엇입니까?

optionbox 2020. 7. 28. 08:26
반응형

Java의 기본 액세스 수정자는 무엇입니까? [복제]


이 질문에는 이미 답변이 있습니다.

명시 적으로 명시하지 않은 경우 메소드 또는 인스턴스 변수의 기본 액세스 수정자는 무엇입니까?

예를 들면 다음과 같습니다.

package flight.booking;

public class FlightLog
{
    private SpecificFlight flight;

    FlightLog(SpecificFlight flight)
    {
        this.flight = flight;
    }
}

이 생성자의 액세스 수정자가 보호 또는 패키지입니까? 동일한 패키지의 다른 클래스, 즉 flight.booking이 생성자를 호출 할 수 있습니까 ?


Java 문서에서

클래스에 수정자가없는 경우 (기본값, 패키지 전용이라고도 함) 자체 패키지 내에서만 볼 수 있습니다 (패키지는 관련 클래스의 그룹으로 명명됩니다. 이후 학습에서 해당 클래스에 대해 배우게됩니다).

에서 최상위 클래스와 동일한 의미로 member levelpublic 수정 자 또는 no modifier(package-private) 을 사용할 수도 있습니다 .

전체 기사 당신은 여기에서 읽을 수 있습니다 ( 최근에 쓴 것 ) :

http://codeinventions.blogspot.com/2014/09/default-access-modifier-in-java-or-no.html


설명서에서 :

Access Levels
Modifier        Class    Package    Subclass    World
-----------------------------------------------------
public           Y        Y          Y           Y
protected        Y        Y          Y           N
(Default)        Y        Y          N           N
private          Y        N          N           N

상황에 따라 다릅니다.

수업 시간에있을 때 :

class example1 {

    int a = 10; // This is package-private (visible within package)

    void method1() // This is package-private as well.
    {
        -----
    }
}

인터페이스 내에있을 때 :

interface example2 {

    int b = 10; // This is public and static.
    void method2(); // This is public and abstract
}

기본 액세스 수정자는 package-private입니다-동일한 패키지에서만 볼 수 있습니다


여기에 당신을 위해 이것을 많이 요약 해야하는 코드 샘플이 있습니다 ... 아래 외에도 다른 패키지의 기본값에 액세스 할 수없는 방법이 하나 더 있습니다.

서브 클래스의 서브 클래스가 다른 패키지에 있으면 서브 클래스에서 디폴트에 액세스 할 수 없지만 서브 클래스가 동일한 패키지에 있으면 액세스 할 수 있습니다.

package main;

public class ClassA {
    private int privateVar;
    public int publicVar;
    int defaultVar;
}

package main;

public class ClassB {
    public static void main(String[] args) {
        ClassA a = new ClassA();
        int v1 = a.publicVar;   // Works
        int v2 = a.defaultVar;  // Works
        int v3 = a.privateVar;  // Doesn't work

    }
}

package other;

public class ClassC {
    public static void main(String[] args) {
        ClassA a = new ClassA();
        int v1 = a.publicVar;   // Works
        int v2 = a.defaultVar;  // Doesn't work
        int v3 = a.privateVar;  // Doesn't work
    }
}

기본 수정자는입니다 package. 동일한 패키지의 코드 만이 생성자를 호출 할 수 있습니다.


Yes, it is visible in the same package. Anything outside that package will not be allowed to access it.


Your constructor's access modifier would be package-private(default). As you have declared the class public, it will be visible everywhere, but the constructor will not. Your constructor will be visible only in its package.

package flight.booking;

public class FlightLog // Public access modifier
{
    private SpecificFlight flight;

    FlightLog(SpecificFlight flight) // Default access modifier
    {
        this.flight = flight;
    }
}

When you do not write any constructor in your class then the compiler generates a default constructor with the same access modifier of the class. For the following example, the compiler will generate a default constructor with the public access modifier (same as class).

package flight.booking;

public class FlightLog // Public access modifier
{
    private SpecificFlight flight;
}

The Default access modifier is package-private (i.e DEFAULT) and it is visible only from the same package.


Default access modifier - If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes).


No, you can't call the default access level to the other package. But you have the access within the package. Follow this link for more details.


Is the access modifier of this constructor protected or package?

I think implicitly your constructors access modifier would be your class's access modifier. as your class has public access, constructor would have public access implicitly


From a book named OCA Java SE 7 Programmer I:

The members of a class defined without using any explicit access modifier are defined with package accessibility (also called default accessibility). The members with package access are only accessible to classes and interfaces defined in the same package.

참고URL : https://stackoverflow.com/questions/16164902/what-is-the-default-access-modifier-in-java

반응형