Reputation: 537
Ok, I am working on an assignment for school, and I set up my main class and also another class called Transaction. In my main class I have:
Transaction t = new Transaction();
And Transaction is underlined: it says that the constructor undefined. WHY?!
The Transaction class looks like this:
public class Transaction {
private String customerNumber, fName, lName, custAddress, custCity;
private int custZip, custPhone;
/** Constructor*/
public Transaction(String a, String b, String c, String d, String e, int f, int g){
this.customerNumber = a;
this.fName = b;
this.lName =c;
this.custAddress = d;
this.custCity = e;
}
It looks like it should just work, but it's just not. Even when I plug in a bunch of variables into where I make the new Transaction object in main, it still says undefined. Somebody please help!
Upvotes: 3
Views: 6815
Reputation: 3247
You need to make a default constructor (one that takes no arguments).
Upvotes: 2
Reputation: 44821
This is because you haven't declared a constructor with no arguments.
When you have no constructor defined at all, there is a default constructor with no arguments defined automatically for you.
But now that you've declared a constructor with arguments, you now need to pass them or declare another constructor with no arguments.
Upvotes: 2
Reputation: 1
Those guys that said that you that there is no default constructor because you coded a constructor with arguments are thinking C++. That's true for C++ but not for Java. There is no such thing as a default constructor. You have to code any constructor for your class. You don't have to have a constructor if you're not going to construct any objects.
Upvotes: -5
Reputation: 92854
There is no default constructor definition in your class.
When you provide the definition of at least one parameterized constructor the compiler no longer provides you the default constructor.
Upvotes: 9