Reputation: 11
Not sure why when I make an instance as in 2 the complier fails and 3 succes
//Instantiate Class Used To Fill In New Stock Details
CreateStockCodeDetails CreateStockDetailsInput = new CreateStockCodeDetails();
CreateStockDetailsInput.CreateStockCodeDetails(CreateNewStockCode); // (2)
CreateStockDetailsInput.CreateStockDetails(CreateNewStockCode); // (3)
When I name the constructor the same name as the class, it fails. Why?
class CreateStockCodeDetails extends JFrame implements ActionListener {
public void CreateStockDetails(String StockCode) {
// This works
}
}
class CreateStockCodeDetails extends JFrame implements ActionListener {
public void CreateStockCodeDetails(String StockCode) {
// This fails. Why?
}
}
Upvotes: 0
Views: 117
Reputation: 5565
You cannot put a return type next to a constructor. In your second class declaration the constructor would just be:
public CreateStockCodeDetails(String StockCode)
{
}
Now you can create the object by doing this...
CreateStockCodeDetails var = new CreateStockCodeDetails("WTF is a stock code");
The return type is supposed to be implicit on constructors since you always know what type you are constructing....
Your first class declaration works because the method you've declared is not a constructor(since it both has a return type and is NOT the same name as the class), so it is treated as such with a return type of void.
Upvotes: 4