简体   繁体   中英

how to give reference for static inner class while creating object?

I have classes below, I will create object for inner class however I have doubt on creating object for inner class.

class parent{

    String name;

    public static class inner {
        String name;
    }
}

class Main {
    parent p = new parent();
    parent.inner in = new inner() //when do i have to create like this?
    parent.inner in = new Parent.inner()  //when do i have to create like this?

}

I have created two ways to create object for inner class but what is the different between those? which one is correct?

if both are correct then which scenario i have to create like above?

First of all you can't access parent.inner in = new inner() in you Main class Because you can't directly access this class member without class name or Object

go through this link this

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();  //static classes
OuterClass.InnerClass innerObject = outerObject.new InnerClass(); // non-static classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM