简体   繁体   中英

Java: Instantiated object variable error

i have a problem that i'm afraid is very simple, yet i can't figure it out even with the help of lectures and tutorials.

I have this piece of code to create a class with some variables:

public class Symbol
{
    public String sign;
    public boolean win;
}

I then want to instantiate the class as an object and set its variables to a certain value like this:

Symbol x = new Symbol();
x.sign = "Rock";
x.win = true;

I did this exactly like the lecture i took said, but still i get the following error:

"<identifier> expected"

What am i doing wrong? There was no identifier declared in any example i looked at. I am breaking my head over this for several hours now and - as embarresing as that is - i am at my wits end. Please help.

On a side note: I am using BlueJ to compile and run the code - if that is of any relevance.

Thanks a lot

You're missing a semicolon:

x.sign = "Rock"

Should be

x.sign = "Rock";

EDIT:

If that's a typo, then your code works fine.

I think you have written initialization code outside main method (directly in class)

public class Symbol
{
    public String sign;
    public boolean win;

    public static void main(String[] args) {
        Symbol x = new Symbol();
        x.sign = "Rock";
        x.win = true;
    }
}

Copy in main method.

You were all right.

The problem was, infact, with BlueJ.

When I ran the code with another editor it worked as intended. I guess I know what I take away from this one ...

Still, thanks for all your answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM