简体   繁体   中英

How to instance the nested class when have a same method name as class name?

Code first.

class A
{
    private A(){}
    public static A.Builder Builder()
    {
        /**
         * ERROR:
         * No enclosing instance of type A is accessible. 
         * Must qualify the allocation with an enclosing instance of type A 
         * (e.g. x.new A() where x is an instance of A).
         */
        return new A.Builder();
        // Error too
        //return new Builder();
    }

    public class Builder
    {
        private Builder()
        {}
    }
}

Q: How to instance the builder but do not change the static Builder and nested class name ?

EDIT

If the class is static, how to save the date for every builder ? how to chain the build process ?

public static class Builder
{
    private Builder()
    {}

    public Builder add(int a)
    {
        return this;// how to chain the build process ?
    }
    public Builder add(float a);
    public List<Double> Build();
}

OK, I should google java builder pattern first. Here is an example.

: If an inner class is used outside of the enclosing class, it must be static. :如果在封闭类之外使用内部类,则它必须是静态的。

public static class Builder
{
    private Builder()
    {

    }
}

It's by design.

To make it compile without making the nested class static, you would need an instance of A:

A a = new A();
return a.new Builder();

Or shorter version:

return new A().new Builder();

But it would probably make more sense to use a nested static class instead of an inner class, so you can instantiate a new Builder without having to create a new A..

Your code has several errors or bad practices. The name of the class has nothing to do with your problem.

Constructors look like methods but they are not. You may think this is a method called Builder but it is not, it is a constructor, constructors don't have return type + name but just the name of the class:

public class Builder
{
    private Builder() // <-- CONSTRUCTOR
    {}
}

But this is a static method:

class A
{
    public static A.Builder Builder() <-- METHOD (with return type and name)
        { ... }
}

A method name should start with a lower case letter (this do not affect constructors) so this is a mistake, it should be named builder() .

The compilation problem means you are trying to create an object of a non static inner class inside a static context ( the static method Builder of class A ). You cannot do this because non static inner classes instances must be bound to a instance of the outer class.

If you don't want A.Builder instances to be related to A instances then make it static. If you want them to be bound then create the instance of A.Builder inside a non static method (or constructor) of A or use this syntax A a = new A(); Builder = a.new Builder(); A a = new A(); Builder = a.new Builder(); as pointed by assylias . In both cases you get a new instance of Builder bound to an instance of A.

Actually, what I need is a static class which is different from C#'s static class. Here is an good explain. If a nested class is not static, it need a instanced container class. This is how I understand the static class in java.

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