Question

I had a syntax error one the line with the serialVersionUID. to fix this error i had to place a bracket at the end of that line and close it at the end of my code... my question is Why? There was no need for this in the file that contains the Jframe... also what is serialVersionUID? I apologize if my questions seem elementary, I'm new to Programming, newer to java, and this is my 3ed day on GUI's.

import javax.swing.*;


public class HangmanPanel extends JPanel{
    private static final long serialVersionUID = -1767262708330188227L;{

    this.setLayout(null);
    JLabel heading = new JLabel("Welcome to the Hangman App");
    JButton Button = new JButton("Ok");
    //get input

    JLabel tfLable = new JLabel("Please Enter a Letter:");
    JTextField text = new JTextField(10);


    heading.setSize(200, 50);
    tfLable.setSize(150, 50);
    text.setSize(50, 30);
    Button.setSize(60, 20);


    heading.setLocation(300, 10);
    tfLable.setLocation(50, 40);
    text.setLocation(50, 80);
    Button.setLocation(100, 85);

    this.add(heading);
    this.add(tfLable);
    this.add(text);
    this.add(Button);
}}
Was it helpful?

Solution

Your problem has nothing to do with serialVersionUID. Go ahead, delete that entire line; you'll see that you still need the braces.

Your problem is that you're writing code outside of any function. Therefore, Java considers it to be an instance initializer, and instance initializers must be surrounded by braces.

The easiest-to-understand solution is to create a constructor to contain your code:

public class HangmanPanel extends JPanel{
    private static final long serialVersionUID = -1767262708330188227L;

    public HangmanPanel () {

        this.setLayout(null);
        JLabel heading = new JLabel("Welcome to the Hangman App");
        JButton Button = new JButton("Ok");

        // and so on

From a purely behavioral perspective, adding an explicit constructor in this case does nothing: instance initializers are invoked as part of object construction. However, they are confusing (as you've shown by your question). Adding the constructor clears up the confusion.

OTHER TIPS

Your syntax error is caused because you've written code directly in a class file.

Place your code in a constructor instead:

public HangmanPanel() {
  // code here...
}

By using braces, you inadvertently created an initializer block. These are typically only used when one is creating multiple constructors for a class and have a piece of common code that should be executed by all of them.

No, you are misled.

The inner brackets form an initializer block, something like a constructor without name. All such initializer blocks are called at construction.

public class A {

    public List<String> names; // Field
    { // Initializing block.
        Collections.addAll("John", "Elma");
    }

    // Constructor
    public A() {
    }

    public void f() { // Method
    }
}

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.

You had a problem because you do not put your code in function/constructor. To define the scope of your code you have added curly braces. Put the code in function.

The error and solution reported is slightly misleading: i.e. the first line has correct syntax:

private static final long serialVersionUID = -1767262708330188227L;

The next word is the keyword 'this' which must be written within the scope a instance method. Adding the brackets as per the suggestion creates an anonymous initialiser which is syntactically correct but NOT what you want to do.

As suggested by Duncan - write your instance initialisation code within an instance method/constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top