Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: John Cooper   on Feb 28 In Java Category.

  
Question Answered By: Alisha Johnson   on Feb 28

The first (default) contructor is there so you can
create instances of your class  in the simplest way
possible.
ie.

Die d = new Die();

this will set  the values of the member data to their
default values. The creator of this class could have
initialized the values during declaration as you
described and could have created an empty default
constructor instead. This is a matter of taste.

ie.

private int  numFaces = 6;
private int faceValue = 1;

public Die(){}

Remember that if you create any contructor(s) a
default one is not created for you. Thus, if we were
to omit the default constructor  you would not be able
to make an instance of the class in the way described
above. You would have to use the constructor that
takes arguments. ie.

Die d = new Die(); // this will not compile
Die d = new Die(6); // you will have to do this

Share: