1. 内部クラス

最近、静的変数と静的メソッドがあることを学びました。静的クラスも存在することがわかりました。しかし、私たちはこのテーマに少し離れたところからアプローチしていきます。

Java では、クラス内でクラスを宣言することが完全に許可されています。そして、クラス内のクラス内のクラス内のクラスさえも。すべては非常に単純に見えます。

class OuterClass
{
   variables of the class
   methods of the class

   class NestedClass
   {
      variables of the class
      methods of the class
   }
}

あるクラスを別のクラスの中で宣言するだけです。とてもシンプルです。

例:

public class Solution
{
   static ArrayList<Point> points = new ArrayList<Point>();

   public static void main(String[] args)
   {
      Point point = new Point();
      point.x = 100;
      point.y = 200;
      points.add(point);
   }

   static class Point
   {
      int x;
      int y;
   }
}

ネストされたクラスは静的または非静的になります。静的に入れ子になったクラスは、単に「静的に入れ子になったクラス」と呼ばれます。非静的ネストされたクラスは、内部クラス(内部クラス) と呼ばれます。



2. 静的クラス

静的に入れ子になったクラスは、外部クラスの外部で使用できます。このようなクラスに public アクセス修飾子がある場合は、プログラム内のどこでも使用できます。このようなクラスは通常のクラスとほとんど区別がつきません。ただし、いくつかの違いがあります。

クラス名

外部クラス以外の場所から静的ネストされたクラスを参照する場合は、外部クラスの名前とネストされたクラスの名前で構成されるクラス名を指定する必要があります。この名前の一般的な外観は次のとおりです。

OuterClass.InnerClass

例:

アウタークラス 入れ子になったクラス ネストされたクラスの完全名
com.codegym.Solution
Point
com.codegym.Solution.Point
java.util.Map
Entry
java.util.Map.Entry
java.util.Files
DirectoryStream
java.util.Files.DirectoryStream
java.nio.WindowsPath
Closeable
java.nio.WindowsPath.Closeable

ネストされたクラスに独自のネストされたクラスがある場合、それらの名前はドットを使用して単純に結合されます。

JDK のネストされたクラスの典型的な例は、クラスEntry内のMapクラスです。オブジェクトに格納されているキーと値のペアのセットを取得する場合はHashMap、 をentrySet()返すメソッドを使用します。Set<Map.Entry>

は、外部クラスとネストされたクラスの例であることに注意してください。Map.Entry

オブジェクトの作成

静的ネストされたクラスのオブジェクトを作成するのは非常に簡単です。見た目はこんな感じです。

OuterClass.NestedClass name = new OuterClass.NestedClass();

通常のクラスとすべて同じですが、名前は 2 つの部分で構成されます。

静的メソッドの呼び出し

静的クラスに静的メソッドがある場合は、通常のクラスの静的メソッドと同じ方法でアクセスできます (ただし、クラス名は 2 つの部分で構成されます)。

OuterClass.NestedClass.staticMethod();

静的変数へのアクセス

ネストされたクラスのパブリック静的変数へのアクセスも簡単です。

OuterParent.NestedClass.nameOfStaticVariable


3. 静的クラスの特徴

静的に入れ子になったクラスには、静的と呼ばれる理由がほとんどありません。これらは通常のクラスと同様に動作します。非静的メソッドからのアクセスには制限はありません。

外側のクラス内で静的にネストされたクラスを操作している場合、最も一般的な (ネストされておらず、静的でもない) クラスとの違いはわかりません。

例:

静的なネストされた Point クラス 通常のポイントクラス。
public class Solution
{
   static ArrayList<Point> points;

   public static void main(String[] args)
   {
      Point point = new Point();
      point.x = 100;
      point.y = 200;
      points = new ArrayList<Point>();
      points.add(point);
   }

   static class Point
   {
      int x;
      int y;
   }
}
public class Solution
{
   static ArrayList<Point> points;

   public static void main(String[] args)
   {
      Point point = new Point();
      point.x = 100;
      point.y = 200;
      points = new ArrayList<Point>();
      points.add(point);
   }
}

class Point
{
   int x;
   int y;
}

静的にネストされたクラスを取得し、それをその外部クラスから移動した場合、唯一変わることは、新しいクラスが以前の外部クラスの変数とメソッドにアクセスできなくなることです。private static

例:

静的なネストされた Point クラス 通常のポイントクラス。
public class Solution
{
   private static ArrayList<Point> points;

   static class Point
   {
      int x;
      int y;

      public static void main(String[] args)
      {
         Point point = new Point();
         point.x = 100;
         point.y = 200;

         // This will work
         points = new ArrayList<Point>();
         points.add(point);
      }
   }
}
public class Solution
{
   private static ArrayList<Point> points;
}

class Point
{
   int x;
   int y;

   public static void main(String[] args)
   {
      Point point = new Point();
      point.x = 100;
      point.y = 200;

      // This will generate an error
      points = new ArrayList<Point>();
      points.add(point);
   }
}

main通常のPointクラスのメソッドはクラスの変数にアクセスできませんprivate static pointsSolution

これが、静的ネストされたクラスと通常のクラスの主な違いです。静的ネストされたクラスのメソッドは、宣言されている場合でも、外部クラスのすべての静的変数とメソッドにアクセスできますprivate

考えてみれば、なぜそれが驚くべきことでしょうか? このprivate修飾子は、この修飾子でマークされた変数とメソッドにはそのクラス内からのみアクセスできることを明示的に示します。静的ネストされたクラスはその外部クラスの中にありますか? はい、それで問題ありません!好きなだけアクセスしてください。