Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is eclipse generating an Syntax error here?

Tags:

java

syntax

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 :/

like image 849
Christian Avatar asked Aug 29 '26 16:08

Christian


2 Answers

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).

like image 89
Simon Forsberg Avatar answered Sep 01 '26 05:09

Simon Forsberg


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");
        }};
    }
}
like image 23
Joshua Taylor Avatar answered Sep 01 '26 06:09

Joshua Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!