1

I am writing a Java (Swing) application and I'm trying to store stuff in an ArrayList. My plan is to first instantiate a class, set some variables in that class and then add that class to an ArrayList.

So I have this class:

public class CollectionClass {

private ArrayList<OwnerClass> owners;

public CollectionClass() {

    owners = new ArrayList<OwnerClass>();

}


public void AddOwner(OwnerClass oc) {
    owners.add(oc);
}
 }

And this class:

public class OwnerClass {
public int id = 0;
public String name = "";
public String employeeNr="";
 }

Now in my dialog I have this:

CollectionClass myCC;

public InvoerNewOwner(CollectionClass cc) {

            myCC = cc;
        btSave.setActionListener(new ActionListener() {
        OwnerClass oc = new OwnerClass();

        oc.name = txtOwner.getText();               <<<
        oc.employeeNr = txtEmployeeNr.getText();    <<<

        myCC.addOwner(oc);                            <<<

    });

I get an error on the 3 lines marked with <<<. The error is "error: <identifier> expected"

What am I doing wrong here?

Is this not a good way to create a collection of objects?

tckmn
  • 57,719
  • 27
  • 114
  • 156
Eric
  • 695
  • 9
  • 25
  • 1
    Use setters to set variable value. – BobTheBuilder Feb 10 '13 at 14:11
  • @baraky **NO!** Setters and getters are **highly** overused and in this case are pointless. – tckmn Feb 10 '13 at 14:12
  • @Doorknob Sorry but you are highly wrong. Getters&Setters are never overly used. Saying the contrary is the proof of lack of experience. public accessors should be forbidden and goes against all good OO-practices. See this [question](http://stackoverflow.com/questions/565095/are-getters-and-setters-evil) for more info. – Guillaume Polet Feb 10 '13 at 16:00
  • @GuillaumePolet Indeed they are overused, I avoid them at all times. Does there really need to be a `setMyCC(CollectionClass c) {myCC = c;}`? No, there doesn't. – tckmn Feb 10 '13 at 16:03
  • @Doorknob Thanks for sharing your thoughts. Yet, I can only encourage you to read the answers provided in the question I mentionned in my previous comment. You will actually see that many people disagree with your point of view. Encapsulation and hiding data as much as possible is one of the very basic principles of OO-programming. Will code work if you don't follow it? Yes sure it will. But it makes your code more fragile, less maintainable and harder to debug. – Guillaume Polet Feb 10 '13 at 16:27
  • @GuillaumePolet I am talking about this case. In my last comment, is that function really necessary? – tckmn Feb 10 '13 at 16:29
  • @Doorknob Necessary? No. But is it a good practice to write quality-code right away? Yes! Why would you create poorly-written code if for the same "cost" you can write "good" code directly? Anyway, I feel like we won't agree on this subject. Cheers. – Guillaume Polet Feb 10 '13 at 16:40

1 Answers1

6
btSave.setActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        OwnerClass oc = new OwnerClass();

        oc.name = txtOwner.getText();
        oc.employeeNr = txtEmployeeNr.getText();

        myCC.addOwner(oc);
    }
});

You must wrap all of the things you want to do inside an actionPerformed event.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 1
    Doorknob, when you ask a question and people test your code and comment, you're being very rude if you delete your question without explanation. – Denys Séguret Feb 10 '13 at 16:45
  • @dystroy I realized I was passing `0` to the function. I didn't need the question then so I deleted it. – tckmn Feb 10 '13 at 16:56