How do I define a single constructor public packet(String[] biscuit) which makes my field from private String[] biscuitList to private String[] biscuit?
-
I don't understand what you mean by "makes my field from ... to ...". Please elucidate.Steve Emmerson– Steve Emmerson2010-02-08 00:49:19 +00:00Commented Feb 8, 2010 at 0:49
-
What do you want to do? Example code will help too.mtmk– mtmk2010-02-08 00:51:49 +00:00Commented Feb 8, 2010 at 0:51
-
sorry for the confusion, let me word it better, what im trying to do is to set a single constructor that will change the ending of 1 of my fields e.g. from "biscuitList" to "biscuit" cheersuser267935– user2679352010-02-08 00:55:26 +00:00Commented Feb 8, 2010 at 0:55
2 Answers
Just assign it to the field.
public class Packet {
private String[] biscuitList;
public Packet(String[] biscuit) {
this.biscuitList = biscuit;
}
}
The this refers to the current Packet instance (which you just created using new Packet). The this.biscuitList refers to the biscuitList field of the current Packet instance. The = biscuit assigns the given biscuit to the left hand (which in this case is the biscuitList field.
That said, a String[] variable shouldn't really be called with a name ending in List. This may cause ambiguity with a List<String>. You can just call it biscuit, or maybe better, biscuits.
public class Packet {
private String[] biscuits;
public Packet(String[] biscuits) {
this.biscuits= biscuits;
}
}
Also, classnames and constructor names ought to start with uppercase. I.e. Packet and not packet.
To learn more about Java, check the Trials Covering the Basics.
2 Comments
this.biscuitList = biscuit;