Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for 'new' in java [duplicate]

Constructors of non static member classes take an extra hidden parameter which is a reference to an instance of the immediately enclosing class. There is also a syntactic extension of 'new'.

In the below code,

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();
}

Can you please help me understand the meaning of Kb() in K.Ka.Kb().new Kc().new Kd()? I understand that new Kc() is required as told in first paragraph.

like image 583
overexchange Avatar asked Sep 08 '15 14:09

overexchange


People also ask

What is new () in Java?

The new operator is used in Java to create new objects. It can also be used to create an array object. Let us first see the steps when creating an object from a class − Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object.

What is clone () in Java?

Object cloning refers to the creation of an exact copy of an object. It creates a new instance of the class of the current object and initializes all its fields with exactly the contents of the corresponding fields of this object. Using Assignment Operator to create a copy of the reference variable.

How do you duplicate an object in Java?

The clone() method of the class java. lang. Object accepts an object as a parameter, creates and returns a copy of it.


1 Answers

The parentheses you point out actually do not apply to Kb but K.Ka.Kb.

new K.Ka.Kb()

is creating a new instance of the K.Ka.Kb nested class.

like image 195
Tunaki Avatar answered Oct 11 '22 13:10

Tunaki