0

The problem should be on the createClass() method... is it wrong to use classes.add(this)?

private static ArrayList<VirtualClass> classes = new ArrayList<VirtualClass>();
private boolean isPrivate;
private String className;
private String methodName;

public void setVirtualClass(String name, String method, boolean isP){
    this.className = name;
    this.isPrivate = isP;
    this.methodName = method;
}

public void createClass(String name, String method, boolean isP){
    this.className = name;
    this.isPrivate = isP;
    this.methodName = method;
    classes.add(this);
}

More details on the problem: Failed to store values in ArrayList of class object. (CODE EDITED)

Community
  • 1
  • 1
Sue
  • 199
  • 1
  • 3
  • 10
  • what do you want to add to the "classes" list ? – Ankit Rustagi Oct 27 '13 at 13:03
  • 1
    Don't ask the same question again and again. Learn to use a debugger, and step through the code to understand what is actually executes. It's not inherently wrong, but it doesn't look like good design. Static mutable variables are always a smell anyway. – JB Nizet Oct 27 '13 at 13:04
  • Your question should be self-contained: what's the problem? – Robin Green Oct 27 '13 at 13:09

2 Answers2

1

I suppose you want to keep a record of all created classes?

It is generally bad practice to pass out this in a constructor because, by definition, this hasn't been constructed yet. In this instance I don't think it will cause problems but it certainly doesn't smell right.

I would consider using a static factory method or another form of factory pattern so that you can split up object creation and the storing of instances.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

No one can tell you whether it is right or wrong unless you tell what you are trying to do.

If what you are doing is something like : (assume your class is called Foo)

Foo foo = new Foo();

foo.createClass("Blablabla", "method1", true);
foo.createClass("AnotherClass", "method2", true);

something like that, then yes, you are probably wrong. Because what you are doing is simply changing the only Foo instance with different attribute, and adding the same object to the list.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131