开发者

Confusing instance of

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?
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜