Syntax for the "new" in java

Constructors of non-stationary member classes accept an additional hidden parameter, which is a reference to an instance of the directly incoming class. There is also a syntax extension for the "new".

In the code below

class K{
    static class Ka{
        static class Kb{
            class Kc{
                class Kd{

                }
            }
        }
    }
}

class Test{
    K.Ka.Kb.Kc.Kd k = new K.Ka.Kb().new Kc().new Kd();
}

Could you help me understand the meaning Kb()of K.Ka.Kb().new Kc().new Kd()? I understand what new Kc()is required, as stated in the first paragraph.

+4
source share
3 answers

It calls the constructor Kb. This is easier to show in three statements:

K.Ka.Kb x1 = new K.Ka.Kb();
K.Ka.Kb.Kc x2 = x1.new Kc(); // Pass x1 as the hidden constructor arg
K.Ka.Kb.Kd.Kd k = x2.new Kd(); // Pass x2 as the hidden constructor arg
+3
source

, , Kb, K.Ka.Kb.

new K.Ka.Kb()

K.Ka.Kb.

+7

Kb()is the default constructor for the class Kb. This is what is related to the first newline:

  • you create a new instance Kb(the class K.Ka.Kbis actually, depending on the context you can omit K.Ka.)
  • on which you call new Kc()to create a new instanceKc
  • on which you call new Kd()to create a new instanceKd
+1
source

Source: https://habr.com/ru/post/1606431/


All Articles