views:

95

answers:

4

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!

+4  A: 

Because you haven't declared a constrictor with no arguments, when you have no constrictor at all defined there is a default one with no arguments. Because you've declared one with arguments you now need to pass them or declare another with no arguments.

Tom
Constrictor... subconcious leak? :)
Geoffrey Zheng
Bloody iPad Spellcheck
Tom
+9  A: 

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.

Prasoon Saurav
+2  A: 

Please make one default contructor....

Sanju
A: 

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.

JD Williams
In Java you do get a default constructor with no arguments if you specify no constructors.
Matthew Wightman
I experimented. Are you saying that myClass mc = new myClass(); can be used if you haven't coded the no argument constructor? That didn't work when I tried it.
JD Williams
@JD Williams: The default constructor only exists if you specify no other constructors at all, not just no constructors with no arguments. See http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.9 , which defines the default constructor.
Matthew Wightman
Thanks for the link, Matthew. I went back to see what I had done wrong with my experiment. I do see now that it's only if no constructors are defined that a default is provided. My mistake was in sloppy reading of the problem...since he had a constructor with arguments then he needed to provide a default. If he had not coded any constructors then the compiler would provide one. Btw, I registered on the site minutes before making that first comment. Not a very good start but I do appreciate getting me understanding improved.
JD Williams