Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Constructors

  Asked By: John    Date: Feb 28    Category: Java    Views: 790
  

I'm confused with the code below. It has two "constructors"?
First question, can a java code have two "constructors"?
Second, Why it has to have two constructors? The first constructor makes
non-sense to me, it sets the value of numFaces to 6 and faceValue to 1. But
why doesn't it set the value to 6 and 1 right at initializations, like:
private int numFaces=6;
private int faceValue=1;
And why it needs "constructors" to set the values?

What do constructors usually do anyway??
Thanks a lot,
Jenny


public class Die
{
private final int MIN_FACES = 4;

private int numFaces; // number of sides on the die
private int faceValue; // current value showing on the die

public Die () //I'm guessing this is the first constructor?
{
numFaces = 6;
faceValue = 1;
}

public Die (int faces) //I'm guessing this is the second constructor?
{
if (faces < MIN_FACES)
numFaces = 6;
else
numFaces = faces;

faceValue = 1;
}

public int roll ()
{
faceValue = (int) (Math.random() * numFaces) + 1;
return faceValue;
}

public int getFaceValue ()
{
return faceValue;
}
}

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Alisha Johnson     Answered 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

 
Answer #2    Answered By: Varick Fischer     Answered On: Feb 28

Are you realy, realy sure that it doesn't make a default constructor?

public class  Test {
public static void main(String[] args) {
Test t = new Test();
t.helloWorld();
}
public void helloWorld(){
System.out.println("hi");
}
}

output ->
hi
Process terminated with exit code  0

 
Answer #3    Answered By: Haboos Kauser     Answered On: Feb 28

I said if you create any constructor(s).

Try this.

public class  Test {
public Test(int i){
// does nothing
}
public static void main(String[] args) {
// this will not compile
// Test t = new Test();
// you need to use the other constructor
Test t = new Test(1);
t.helloWorld();
}
public void helloWorld(){
System.out.println("hi");
}
}

 
Answer #4    Answered By: Bama Cohen     Answered On: Feb 28

yes one java  class can have more then one construtors
but atleast one constructor..
contructor is called when we create object of the
purticular classs..te first constructor  set the values
of the object instances ...for eg we crete a object of
the calss and set  the values the instances of the
object in the same step so we do this

public Die object1=new Die(); calling first
constructor over here after the caling this step the
values of

object1.myFaces=6 ///its set by the constructor
object1.faceValue=1 //its also set by construtor

the first contructor is zero paramiterize constructor
or default construtor
and the second constructor is parametrize constructor
its get the value from the user and then set it..

the name of the constructor is same the name of the
class wirh case sensitivity and contructors have no
return type..

 
Answer #5    Answered By: Hadil Khan     Answered On: Feb 28

> First question, can a java code  have two "constructors"?

Yes ... A class  can have as many constructors  as it needs.

The most used example of multiple constructors is a constructor  with no
arguments and a constructor with arguments.

Constructors with no arguments are there so you can simply call that
Object and then fill all the data you self ie:

Object o = new Object();
o.someMethod(someData);

While Constructors that accept arguments fill in some level of data ie.

Object o = new Object(someData);

> And why it needs "constructors" to set  the values?

It doesn't need a constructor to set the values, it could force the
programmer to construct all the information.

Lets assume that you had another constructor that was like this

Die () {
}

This is called a nullery constructor (because it does nothing).

To call this object you code

Die d = new Die();

Now this method doesn't set any values. To set those values you would
have to do this;

d.numFaces = 6;
d.faceValue = 1;

> What do constructors usually do anyway??


Constructors are basicly the first thing that is called when you
"construct" a object.

In your case when you make a Die Object you have two ways of making it.
One that has 6 sides and one that can have more than 4 sides.

In this case you can use the constructors to be a default and a
non-default mode.

Die d = new Die(); <- creates a standard 6 sided die
Die d = new Die(255); <- creates a 255 sided die.

 
Answer #6    Answered By: Dale Jones     Answered On: Feb 28

Constructors r been used to execute the code  that have to b executed as soon as
the object is been created or instansiated.



So the Die constructor  is called when we create an object

public Die ()
{
numFaces = 6;
faceValue = 1;
}


ie Die d =new Die();


The second constructor the below one is called when i created an object this way
Die d=new Die(12);

public Die (int faces)
{
if (faces < MIN_FACES)
numFaces = 6;
else
numFaces = faces;

faceValue = 1;
}

 
Didn't find what you were looking for? Find more on Constructors Or get search suggestion and latest updates.




Tagged: