Reputation: 299
I am working on a project, and I was taught to instantiate variables in constructors. I'm having some trouble doing this with an ArrayList thought. Can you suggest some best practices, do I need to define the ArrayList with the instance variables or can I do it in the constructor. Thanks for your suggestions! I have an example of what I'm talking about below:
//imports
import java.util.*;
import java.lang.*;
public class ArrayListConstructorDemo
{
//instance variables/attributes
String string;
List<String> list;// for example does this line need to say List<String> list = new ArrayList<String>();
//constructors
public ArrayListConstructorDemo()
{
String string = "null";
List<String> list = new ArrayList<String>();//is there anyway I can do this here instead of 6 lines up?
}//end default constructor
public ArrayListConstructorDemo(String string,List<String> list)
{
this.string = string;
this.list = list;
}//end generic constructor
//observers/getters/accessors
public String getString(){return string;}//end method getString()
public List<String> getList(){return list;}//end method getList()
//transformers/setters/mutators
public void setTable(String string){this.string = string;}
public void setValues(String list)
{
// for(String s : test)
// {
list.add(this.list);
// }
}
public String toString()
{
return "this is a generic toString method for the class ArrayListConstructorDemo";
}//end toString
public static void main(String[] args)
{
ArrayListConstructorDemo alcd = new ArrayListConstructorDemo();
System.out.println(alcd.list.size());
//test Lists in general
List<String> bleh = new ArrayList<String>();
bleh.add("b1");
System.out.println(bleh.get(0));
}//end method main()
}//end class ArrayListConstructorDemo
Upvotes: 7
Views: 106241
Reputation: 243
How can you do this ??
public void setValues(String list) {
// for(String s : test)
// {
list.add(this.list);
// }
}
There is no method like add() to manipulate Strings, Instead you would have done this :
public void setValues(List<String> list) {
// for(String s : test)
// {
list.add(this.list);
// }
}
And regarding declaring ArrayList in the constructors you can do like this :
String string;
List<String> list;// for example does this line need to say List<String>
// list = new ArrayList<String>();
// constructors
public ArrayListConstructorDemo() {
string = "null";
list = new ArrayList<String>();// is there anyway I can do this here
// instead of 6 lines up?
}// end default constructor
Upvotes: 2
Reputation: 385
If you want to just declare it in the constructor you can have the code:
ArrayList<String> name = new ArrayList<String>();
Otherwise you can declare it as a field, and then initialize it in the constructor.
private ArrayList<String> name;
And then in the constructor:
name = new ArrayList<String>();
Making it a field would be useful, as you would then be able to create accessor/mutator methods in order to retrieve and use the List from different classes, without having to declare it public (which is rarely a good thing).
Upvotes: 5
Reputation: 323
Generally the practice is to declare before the constructor, and initialize in the constructor.
Here's an example:
class myClass
ArrayList<String> strings
public myClass()
{
strings=new ArrayList<String>();
}
Upvotes: 2
Reputation: 2687
java offers you also an Initializing Fields
http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html see Initializing Instance Members
Upvotes: 0
Reputation: 320
Change
List<String> list = new ArrayList<String>();
to
list = new ArrayList<String>();
Upvotes: 5
Reputation: 69269
If you want to declare it in the constructor, then you (most likely) want to declare the outer field, so you want:
list = new ArrayList<String>();
Currently you are shadowing the List<String> list
class variable, meaning that you are creating a new instance of list
, rather than that you are initializing the list
instance variable.
I personally prefer initializing it at declaration time though, so what you previously had. I prefer this to make the code more concise and you most likely won't end up forgetting to initialize it if you teach yourself that habbit.
Upvotes: 2