My Code:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea; //eclipse say Syntax error } expected
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
Cant find the solution, but i think it is embarrassing easy :/
4 Answers 4
I believe you wanted to put some of the code within a constructor, like this:
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea;
public TAFrame() {
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
}
The problem was that you tried to execute arbitrary code outside any method. Once a field has been declared, you need to access it through a method. It is only possible to initialize it on the same line, so you can do like the following:
public class TAFrame {
private JFrame mainFrame = new JFrame("mainFrame");
private JPanel mainPanel = new JPanel();
private JButton button = new JButton("click me");
private JTextArea textArea = new JTextArea(10, 15);
}
I recommend the constructor-approach in this case, but either way you will most need a constructor anyways since you probably want to add an actionlistener to the button (for example).
3 Comments
Most of the answers here have correctly pointed out that you can use initial values for initialization, and that you can use constructors. However, the Java tutorial, Initializing Fields, actually describes two non-constructor ways of initializing fields: (i) initial values; and (ii) initialization blocks.
The following code demonstrates all three methods (and shows both instance and static initialization blocks):
public class InitializationExample {
private int field1 = 1; // in-line initializer
private int field2a;
private static int field2b;
private int field3;
{
field2a = 3; // instance initializer
}
static {
field2b = 3; // static initializer
}
public InitializationExample( final int field3 ) {
this.field3 = field3;
}
}
Using the initialization blocks, you can make a very minor change to your code and have it compile:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea;
{
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
}
Even though this is possible, it isn't incredibly common, so unless you have some particularly good reason for using this, or it's already common in a codebase that you're working on, initial values or constructors are probably a more readable and maintainable option. It's also important to note that, according to 12.5. Creation of New Class Instances from the Java Language Specification, instance initialization code (and initial values) are processed before constructor code runs.
There is one possible benefit to the initialization block approach over constructor based methods. If you did make this a constructor, along the lines of
public TAFrame() {
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
and then introduced another constructor later that takes some arguments, you'll either need to explicitly call the zero-argument constructor from that constructor (constructor chaining), or include initialization assignments in that constructor, too. Using the initialization blocks, you wouldn't need to do either.
Initialization blocks are also handy when you're creating an instance of an anonymous subclass, because you can keep the initialization code visually "inside" the class. You can read up on double brace initialization for more details, but here's a simple example:
import java.util.HashMap;
import java.util.Map;
public class MapInitializationExample {
public static void main(String[] args) {
// initialization code for this Map is visually "inside" the map
final Map<Integer,String> numberNames = new HashMap<Integer,String>() {{
put(1,"one");
put(2,"two");
put(3,"three");
}};
}
}
1 Comment
Because you are putting code outside of a method that does not belong.
This block in particular:
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
The error is a bit deceiving because it looks like private JTextArea textArea; needs a closing brace. But really, the problem is that the next line does not belong there. The next line, mainFrame = new JFrame("mainFrame"); suggests a method has started and the previous block never closed, thus the reference to a missing }.
You have two options:
- Instantiate the objects in-line with their declarations
- Instantiate the objects in a constructor
To Instantiate In-line
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class TAFrame {
private JFrame mainFrame = new JFrame("mainFrame");
private JPanel mainPanel = new JPanel();
private JButton button = new JButton("click me");
private JTextArea textArea = new JTextArea(10, 15);
}
To Instantiate with a Constructor
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea;
public TAFrame() {
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
}
I personally prefer the inline approach as it is far less verbose.
1 Comment
You should do something like this.
public class TAFrame {
private JFrame mainFrame;
private JPanel mainPanel;
private JButton button;
private JTextArea textArea;
public void initComponents() {
mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15);
}
}
Or you can create and instantiate the control there itself.
private JFrame mainFrame = new JFrame("mainFrame");
private JPanel mainPanel = new JPanel();
private JButton button = new JButton("click me");
private JTextArea textArea = new JTextArea(10, 15);