0
0

More than 3 years have passed since last update.

パッケージとアクセス制御

Posted at

無名パッケージに属するクラスは、同じ無名パッケージに属するクラスからしかアクセスできない。
例えば、下記の2つのクラスは同じパッケージ(無名パッケージ)に属しているためOfficeクラスからPersonクラスを使う事ができる。

class Person{}

public class Office{
    Person p;
}

しかし、下記のように仕様を変更し明示的にパッケージ宣言したクラスから、無名パッケージに属するクラスにアクセスしようとするとコンパイルエラーになる。

class Person{}

package ex4; //明示的にパッケージを宣言する

public class Main{
    public static void main(String[] args) {
        Office office = new Office();
    }
 }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0