0

The code in the constructor does not get called. I have to invoke an additional function to call the code.

Test Code:

public static void main(String[] args){
            SourceClass myClass = new SourceClass();
            myClass.Populate();//If this line is removed it does not work
            System.out.println(myClass.GetDescGroup());

        }

Code being tested:

 public class SourceClass {

    static ArrayList<String> line = new ArrayList<String>();
    static ArrayList<ArrayList> description = new ArrayList<ArrayList>();
    static ArrayList<ArrayList> descGroup = new ArrayList<ArrayList>();

    public void SourceClass () {
        //Does not work.
        String testLine = new String("1 v1 v2 v3");
        String[] testLineSplit = testLine.split(" ");
        for (int i = 0; i < testLineSplit.length;i++) {
            line.add(testLineSplit[i]);
        }

        description.add(line);
        descGroup.add(description);     
    }

    public void Populate() {
        //works
        String testLine = new String("1 v1 v2 v3");
        String[] testLineSplit = testLine.split(" ");
        for (int i = 0; i < testLineSplit.length;i++) {
            line.add(testLineSplit[i]);
        }

        description.add(line);
        descGroup.add(description);
    }

    public String GetDescGroup() {
        return descGroup.get(0).get(0).toString();
    }
}

Same code goes in Populate() and SourceClass()

When a function is called to get the ArrayList it just turns up as empty

6
  • 1
    Show us complete code.. Where is it working? where is it not working? Commented May 7, 2015 at 10:39
  • Why do you think you want ArrayList<ArrayList>? Commented May 7, 2015 at 10:40
  • 1
    where is the constructor? Commented May 7, 2015 at 10:40
  • I'll quickly edit my question so it makes more sense Commented May 7, 2015 at 10:42
  • It's very unclear what you're asking. Commented May 7, 2015 at 10:45

2 Answers 2

2

Your method SourceClass has a return type void which makes it a method and not a constructor. So when you create an object with new SourceClass it calls the default empty constructor

The constructor must be :

public SourceClass () {
 ....
}
Sign up to request clarification or add additional context in comments.

Comments

0

When you define a constructor, don't give it a return value.

In you code:

public void SourceClass () {
    //Does not work.
    String testLine = new String("1 v1 v2 v3");
    String[] testLineSplit = testLine.split(" ");
    for (int i = 0; i < testLineSplit.length;i++) {
        line.add(testLineSplit[i]);
    }

    description.add(line);
    descGroup.add(description);     
}

This is NOT a constructor, just a method of the class that has the same name as class and is returning void.

In this case, as Java understands you are NOT providing any constructor, java provides a default one without parameters. In the test code, this default constructor is invoked, not your SourceClass method.

To fix it just remove 'void' from the constructor declaration.

Comments

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.