0

Part of a separate class file for "Bericht"-objects (dutch for messages, contains a string and two dates) :

private ArrayList<Bericht> lijst = new ArrayList<>(); //
//constructor for a new object:
    public Bericht(String bericht, Date startDag, Date eindDag) {
        this.bericht = bericht;
        this.startDag = startDag;
        this.eindDag = eindDag;
       // System.out.println(""+lijst.size())// prints always a "0"
        lijst.add(this);
       // System.out.println(""+lijst.size())//is always one

    }

Somewhere in my main method i create new objects from this class:

 Bericht message = new Bericht(berichtVak.getText(), calendar1.getDate(), calendar2.getDate());
 message.printBerichten(berichtTextArea); //this method prints the String from each Bericht- object in the textarea 

the method "printberichten" iterates the ArrayList and prints all messages

   public void printBerichten(JTextArea jta) {
        StringBuffer bfr = new StringBuffer();
        for (Bericht msg : lijst) {
            bfr.append(msg.getBericht()); //this getter method returns the string
        }
        jta.setText(bfr.toString());
    }

getBericht getter method:

   public String getBericht() {
    return bericht;
}

So if i make a new object, all my previous objects should be displayed in the textArea, but it seems that i only create one instance of my class.

Before and After the command "lijst.add(this)" i added a System.out.println to check the size from the Arraylist and one returns always a 0 and the other a "1".

2
  • 1
    so what is your question? Commented Oct 13, 2014 at 21:22
  • 1
    You might want to make arraylist static. Commented Oct 13, 2014 at 21:22

2 Answers 2

1

From your expectation, you need lijst to be static:

private static ArrayList<Bericht> lijst = new ArrayList<>(); //

Optionally printBerichten could be static as well since it does not access any member variables:

public static void printBerichten(JTextArea jta) {

By making lijst static each instance of Bericht will be added to the single list when it is constructed. When lijist was not static, each instance of Bericht would have its own list and only the single instance would be added to that list.

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

1 Comment

Thanks, learned something new today (ie a static object) now have: private static ArrayList<Bericht> lijst = new ArrayList<>() and that solved my problem.
0

make lijst static:

private ArrayList<Bericht> lijst = new ArrayList<>(); //
    public Bericht(String bericht, Date startDag, Date eindDag) {
        this.bericht = bericht;
        this.startDag = startDag;
        this.eindDag = eindDag;
       // System.out.println(""+lijst.size())
        lijst.add(this);
       // System.out.println(""+lijst.size())

    }

1 Comment

this answer has been flagged as low quality answer, please provide some explanation.

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.