illegal start of expression error

A

aggreen

Hi,

I'm fairly new to java, so I don't even know if what I'm trying to do is
possible, but I keep getting this "illegal start of expression" error
whenever I try to compile this segment of code.

public void actionPerformed(ActionEvent a) {
Object source = a.getSource();

if (source == okayButton) {

public control2_3a() {

blah = new JLabel("blah..");
}

private static void quantitySelector() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Select How Many Products Were
Purchased On...");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent ContentPane2 = new control2_3a();
ContentPane2.setOpaque(true);
frame.setContentPane(ContentPane2);
frame.pack();
frame.setVisible(true);
}

public void run() {
quantitySelector();
}

}
}


The error message is referencing the line: public control2_3a() { ... My
best guess right now is that I can't do what I'm trying to do inside of an
if statement? (Theres more code... thats just the section that has problems
at the moment. I have a button that I'm listening to and everything, but
the program would be way too long to put on here.) Any help would be
appreciated... Oh and also a little side note. It is also giving me an
error message that says it is expecting a semicolon on the very last line
of the section that I posted. If I put a semicolon there it appeases the
compiler... but I'm not really sure why it seems to be necessary.

Thanks,
Alex Green
 
J

Jon Martin Solaas

aggreen said:
The error message is referencing the line: public control2_3a() { ... My
best guess right now is that I can't do what I'm trying to do inside of an
if statement?

Yeah, and not inside another method either. The offending line seems to
be a constructor (as it has no type), and should be defined inside a
class with the same name. You can't define constructors or other methods
inside other methods (actionPerformed). From that follows that you can't
define methods inside ifs, while ... etc. either. You can define classes
inside classes, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top