0

I was under the belief that whenever you create an instance of a static nested class it should be something like:

Outerclass.StaticNestedClass myObject = new Outerclass.StaticNestedClass();

This code below (I omitted some of the code which I don't think was relevant here) doesn't use the enclosing class name. GeneratedData is a static nested class of class ObjectBuilder. Why don't I see:

ObjectBuilder.GeneratedData = ObjectBuilder.createMallet()

Instead there is:

GeneratedData generatedData = ObjectBuilder.createMallet();

here are the java files...

public class Mallet {
public Mallet(float radius, float height, int numPointsAroundMallet) {

    GeneratedData generatedData = ObjectBuilder.createMallet(new Point(0f,
        0f, 0f), radius, height, numPointsAroundMallet);

    // lots of code omitted

}
}    

and here is the class where the static nested class is defined. As you can see, the method createMallet() creates an object of type GeneratedData, and the Mallet class above used that method instead of the constructor.

class ObjectBuilder {

static interface DrawCommand {
    void draw();
}

static class GeneratedData {
    final float[] vertexData;
    final List<DrawCommand> drawList;

    GeneratedData(float[] vertexData, List<DrawCommand> drawList) {
        this.vertexData = vertexData;
        this.drawList = drawList;
    }
}

static GeneratedData createMallet(
    // lots of code omitted
    return builder.build();
}    

private final float[] vertexData;

private ObjectBuilder(int sizeInVertices) {
    vertexData = new float[sizeInVertices * FLOATS_PER_VERTEX];
}

private GeneratedData build() {
    return new GeneratedData(vertexData, drawList);
}
}
2
  • What is the question? Also, please specify the language you are using (Java? C#?) in a tag. Commented Nov 10, 2013 at 22:50
  • sorry, I hope I made it better now? Basically the textbook I am looking at is using static nested classes differently to what I have seen on the Oracle website and I don't know why. Commented Nov 10, 2013 at 22:56

1 Answer 1

2

Because you can import xyz.ObjectBuilder.GeneratedData and use GeneratedData afterwards in the class.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay I see that at the top of the code in the mallet class. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.