JavaRush /Java Blog /Random EN /Static nested classes

Static nested classes

Published in the Random EN group
Hello! We continue to explore the topic of nested classes in Java. In the last lesson we talked about non-static nested classes or, as they are also called, inner classes. Static nested classes - 2Today let's move on to another group and take a closer look at static nested classes. Static nested classes - 3How are they different from other groups? When declaring such a class, we use the already familiar static keyword:
public class Boeing737 {

   private int manufactureYear;
   private static int maxPassengersCount = 300;

   public Boeing737(int manufactureYear) {
       this.manufactureYear = manufactureYear;
   }

   public int getManufactureYear() {
       return manufactureYear;
   }

   public static class Drawing {

       public static int getMaxPassengersCount() {

           return maxPassengersCount;
       }
   }
}
Static nested classes - 4In this example, we have an external class Boeing737that creates an airplane of this model. And he has a constructor with one parameter: the year of manufacture ( int manufactureYear). There is also one static variable int maxPassengersCount- the maximum number of passengers. It will be the same for all aircraft of the same model, so we only need one copy. In addition, it has a static inner class Drawing- airplane blueprint. In this class we can encapsulate all the service information about the aircraft. In our example, for simplicity, we limited it to the year of manufacture, but it can contain a lot of other information. Static nested classes - 5As we discussed in the last lecture, creating such a nested class increases encapsulation and promotes a more realistic abstraction. What is the difference between static and non-static nested classes? 1. A static class object Drawingdoes not store a reference to a specific instance of the outer class. Remember the example from the last lecture with a bicycle:
public class Bicycle {

   private String model;
   private int mawWeight;

   public Bicycle(String model, int mawWeight) {
       this.model = model;
       this.mawWeight = mawWeight;
   }

   public void start() {
       System.out.println("Go!");
   }

   public class SteeringWheel {

       public void right() {
           System.out.println("Steering wheel to the right!");
       }

       public void left() {

           System.out.println("Steering wheel to the left!");
       }
   }

}
There we talked about how SteeringWheela reference to an object of the outer class (bicycle) is passed to each instance of the inner class (steering wheel) without us noticing Bicycle. Without an object of the outer class, an object of the inner class simply could not exist. This is not true for static nested classes. An object of a static nested class can easily exist on its own. In this regard, static classes are more “independent” than non-static classes. The only point is that when creating such an object you need to specify the name of the external class:
public class Main {

   public static void main(String[] args) {

       Boeing737.Drawing drawing1 = new Boeing737.Drawing();
       Boeing737.Drawing drawing2 = new Boeing737.Drawing();
   }
}
Why did we make the class Drawingstatic, but in the last lecture the class Seat(bike seat) was non-static? Like last time, let's add a little "philosophy" to understand the example :) Unlike a bicycle seat, the essence of the drawing is not so tightly tied to the essence of the airplane. A separate seat object, without a bicycle, will most often be meaningless (though not always - we talked about this in the last lesson). The essence of the drawing makes sense in itself. For example, it may be useful to engineers planning aircraft repairs. They don’t need the plane itself for planning, and can be located anywhere - just a drawing is enough. In addition, for all aircraft of the same model, the drawing will still be the same, so there is no such rigid connection as a seat with a bicycle. Therefore, the object Drawingdoes not need a link to a specific aircraft object. 2. Different access to variables and methods of an external class. A static nested class can only access static fields of the outer class. In our example, the class Drawinghas a method getMaxPassengersCount()that returns the value of a static variable maxPassengersCountfrom an external class. However, we cannot create a method getManufactureYear()in Drawingto return a value manufactureYear. After all, a variable manufactureYearis non-static, which means it must belong to a specific instance Boeing737. And as we have already found out, in the case of static nested classes, the object of the outer class can easily be missing. Hence the limitation :) It does not matter what access modifier the static variable in the outer class has. Even if it is private, there will still be access from a static nested class. All of the above concerns not only access to static variables, but also to static methods. IMPORTANT! The word staticin an inner class declaration does not mean that just one object can be created. Don't confuse objects with variables. If we are talking about static variables, yes, a static class variable, for example, maxPassangersCountexists in a single copy. But when applied to a nested class, staticit only means that its objects do not contain references to objects of the outer class. And we can create as many objects as we like:
public class Boeing737 {

   private int manufactureYear;
   private static int maxPassengersCount = 300;

   public Boeing737(int manufactureYear) {
       this.manufactureYear = manufactureYear;
   }

   public int getManufactureYear() {
       return manufactureYear;
   }

   public static class Drawing {

       private int id;

       public Drawing(int id) {
           this.id = id;
       }

       public static int getPassengersCount() {

           return maxPassengersCount;
       }

       @Override
       public String toString() {
           return "Drawing{" +
                   "id=" + id +
                   '}';
       }

       public static void main(String[] args) {

           for (int i = 1; i < 6; i++) {

               Boeing737.Drawing drawing = new Boeing737.Drawing(i);
               System.out.println(drawing);
           }
       }
   }
}
We declared the method main()directly in the nested class (there's no particular reason for this - just so you know it's possible), and created 5 objects Drawing. Despite the fact that we do not have a single object of an external class. As you can see, there were no problems :) Console output:

Drawing{id=1}
Drawing{id=2}
Drawing{id=3}
Drawing{id=4}
Drawing{id=5}
This concludes our lesson! Just in case, I’ll leave you a link to the section about them in the Oracle documentation . Read it if there are any unclear points. Now it's time to solve a couple of problems! :)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION