Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to enter 2 parameters of the same type in a class constructor

 
Nick Petas
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get an error from from java compiler when I am trying to enter a class constructor with 4 parameters 2 of which are of the same type.Any ideas how to solve this?

code :

public Invoice ( String pNumber, String pDescription, int iQuantity, double iPrice )
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code looks fine and it certainly is legal to have multiple parameters of same type. Sounds as if you may have misinterpreted the compiler's complaints. Can you post the compiler output?
 
Nick Petas
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use eclipse with sdk6 compiler.The error i get is that :

Duplicate method Invoice(String) in type Invoice
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's telling you that you have two constructors, both of which take the same parameter list, a String. So somewhere, you have

followed by

The parameter list must be different somehow. You're best bet is to take one out.
[ February 07, 2007: Message edited by: Fred Rosenberger ]
 
Nick Petas
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no other constructor in this class.Thnx for the immediate replies!
 
Nick Petas
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the complete code list so far :

public class Invoice
{
private String partNumber, partDescription; //part number and part description
private int itemQuantity; //item quantity
private double itemPrice; //item price

//constructors initialize all four instance variables
public Invoice ( String pNumber, String pDescription, int iQuantity, double iPrice )
{
partNumber = pNumber; //initializes partNumber;
partDescription = pDescription; //initializes partDescription
itemQuantity = iQuantity; //initializes itemQuantity
itemPrice = iPrice; //initializes itemPrice
} //end constructor1
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles fine, with *no* errors when I tried it.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you're actually compiling the file you think you're compiling? Sometimes, one can get confused about directories. To be sure, rename or move the file you think you're compiling then try the compilation again. If it still compiles, you'd better find that other file!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line does the error point to? (Just double-click on the error in the problems view.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic