This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

two constructors  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I define two constructors in the same class

i.e.
public class Car{
double fuelConsumption,tankSize,fuelInTank;

public Car(double fuelConsumption,double tankSize, double fuelInTank,Locat\
ion location){
this.fuelConsumption=fuelConsumption;
this.tankSize=tankSize;
this.fuelInTank=fuelInTank;
this.location=location;
}
public Car(double fuelConsumption,double tankSize,Location location){
this.fuelConsumption=fuelConsumption;
this.tankSize=tankSize;
this.location=location;
}
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The answer to your question is "yes". Note that you can define one in terms of the other, using less code and ensuring that all the constructors work the same way:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!