0

How do I define a single constructor public packet(String[] biscuit) which makes my field from private String[] biscuitList to private String[] biscuit?

3
  • I don't understand what you mean by "makes my field from ... to ...". Please elucidate. Commented Feb 8, 2010 at 0:49
  • What do you want to do? Example code will help too. Commented 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" cheers Commented Feb 8, 2010 at 0:55

2 Answers 2

3

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.

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

2 Comments

It should be said, that the this-keyword, in the first case, has no effect - it could just as well be left out. Usually, its used when there is a name-clash, and we need to distinguish between the instance-variable, and the local variable. (see the second case, which exactly illustrates this point).
@noob11: It should also be said, sentences start with an uppercase and ends with a dot. No, "i am a noob" is not a valid excuse at all. This makes no sense. You also don't need to state that explicitly in every topic. Just show that you're putting effort in asking the question the smart way. That's really all. Also see catb.org/esr/faqs/smart-questions.html
0
this.biscuitList = biscuit;

2 Comments

so the above code do the following? change the ending of 1 of my fields e.g. from private String[] biscuitList; to private String[] biscuit;
I think you are confused about private class fields and constructor parameters. The above assumes biscutList is your private field and biscuit is your parameter. So we are assigning biscuitList with the value of biscuit, so that you can refer to biscuitList in your methods.

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.