2

I have objects representing columns in a table that I want to declare as final. Some of these columns represent primary keys in my table, and for those columns I want to add them to an array list that gets passed into a function.

How can I declare a mutable list (one that I can add primary key columns to) that contains immutable objects (the column objects themselves)?

14
  • Make the objects immutable by using private and final, then add them into Arraylist Commented Sep 12, 2016 at 23:17
  • are u expecting anything else ? Commented Sep 12, 2016 at 23:18
  • What is the type of your primary keys? Commented Sep 12, 2016 at 23:20
  • @Rishi When I declare the array list, is there a way to know that it will contain immutable objects? Something like ArrayList<final Column> Commented Sep 12, 2016 at 23:21
  • 1
    So - your Column class implementation will need to declare all of its fields as final and only provide getters. Simply declaring a reference to your Column class as final isn't sufficient. Commented Sep 12, 2016 at 23:23

1 Answer 1

2
public final class Column {

private final Integer a;
private final String  b;

public Column(Integer a, String b) {
    this.a = a;
    this.b = b;
}

public Integer getA() {
    return a;
}

public String getB() {
    return b;
}

}

Create column class in this fashion and then add the object to ArrayList. Check this out : http://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

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

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.