0

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.

wener
  • 7,191
  • 6
  • 54
  • 78

4 Answers4

2

Rule: 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.

Georgian
  • 8,795
  • 8
  • 46
  • 87
  • Where you quote from ? effect java ? But you do not answer the question, it is possible to instance there class ? – wener Mar 05 '14 at 11:21
  • @wener It's not a quote, it's personal knowledge. :-) If in doubt, Google it. – Georgian Mar 05 '14 at 11:22
  • I was confused with the C# static class and java static class,[This](http://stackoverflow.com/a/7486050/1870054) dose answered my question~ If a nested class is not static, it need a instanced container class.This is how I understand the static class in java. – wener Mar 05 '14 at 11:44
  • @wener Please post your own answer and accept it so that others can see it. – Georgian Mar 05 '14 at 11:46
1

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..

assylias
  • 321,522
  • 82
  • 660
  • 783
0

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(); as pointed by assylias. In both cases you get a new instance of Builder bound to an instance of A.

Community
  • 1
  • 1
aalku
  • 2,860
  • 2
  • 23
  • 44
0

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.

Community
  • 1
  • 1
wener
  • 7,191
  • 6
  • 54
  • 78