views:

139

answers:

3

I'm learning Java by reading the online Java book, but im finding it hard to understand "constructors". My question is how would I write a constructor that sets private fields of the class to the values received?

I've attempted this but don't think its correct:

public Biscuit(String id, String Biscuitname, int numOfBiscuits);

So if i have example "12002 digestive 83", i need a constructor that sets private fields of the class to these values received

hope this makes sense

+13  A: 

Here you go:

public class Biscuit
{
    private final String id;
    private final String name;
    private final int count;

    public Biscuit(String id, String name, int count)
    {
        this.id = id;
        this.name = name;
        this.count = count;
    }

    // Other code
}

Some notes:

  • As the parameters and fields have the same name, I've had to use this.foo = foo within the constructor. Some people avoid using the same names for parameters and fields, but I don't personally see a problem with this.
  • I've made the fields final and private; fields should almost always be private IMO, and final where possible - if you can make the whole type immutable, so much the better. (This leads to a more functional style of programming, which is easier to reason about and test.)
  • I've changed the names of the parameters - the name is going to be that of the biscuit, so there's no need to specify that in the parameter name, and likewise numOfBiscuits is obviously related to the biscuit.

Does all of that make sense? Do ask more questions if you have them!

Jon Skeet
thank you, just one last thing can u explain "accessor and mutator methods" in layman terms?
accessor = .getXX() mutator = .setXXX(value)
fuzzy lollipop
@noob: those are easy: "accessors" are methods that get values from an object and "mutators" are methods that change values on an object.
Joachim Sauer
Great answer, as you not only answered the question itself, but improved naming and hinted to good practices (private final).
Mnementh
Note also that the final fields that Jon created MUST be initialized from the constructor. Since they are final, their value cannot be changed once the class is constructed. This is a big advantage although it seems restrictive. (I never thought of it this way, but it could be said that a large step in truly understanding programming is when you start understanding that restrictions are more often than not advantages)
Bill K
+3  A: 

Your attempt is good, but lacks implementation. Instead of a semicolon, you should write assignements to the properties separated by semicolons in curly brackets.

Gabriel Ščerbák
+1  A: 

Simply set the fields in the body of the constructor. If the argument names from the constructor are the same as the field names in the class, then you need to prefix the class field name with this. to "disambiguate" it - Like I did with the "id" field below:

class Biscuit {
   private String id;
   private String biscuitname;
   private int numOfBiscuits;

   public Biscuit(String id, String name, String num) {
       this.id = id;
       biscuitname = name;
       numOfBiscuits = num;
   }
}
Eric Petroelje
don't like it, it's inconsistent
Ion Todirel
@Ion - that's the point. I was trying to illustrate the difference between the two options (when the names match the private fields and when they don't)
Eric Petroelje