I just tried Java Swing helloworld, but I found a question with the following code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloWorldFrame extends JFrame {
public static void main(String args[]) {
new HelloWorldFrame();
}
HelloWorldFrame() {
JButton jbtnButton = new JButton("Button 1");
jbtnButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(HelloWorldFrame.this, "You must click at least once!",
"Title", JOptionPane.ERROR_MESSAGE);
}
});
this.add(jbtnButton);
this.setSize(500, 500);
// pack();
setVisible(true);
}
}
So…my question is simple, how come I don’t need to put return type for “HelloWorldFrame()” ?
Is there a reason or is it just a rule in Java?
Because
HelloWorldFrame()is a constructor of theHelloWorldFrameclass, and Java constructors, like constructors in most (all?) languages with objects, don’t have return values.