- Syntax error on token ".", @ expected after this token
Asked Answered
P

5

5

My Eclipse worked fine a couple of days ago before a Windows update. Now I get error messages whenever I'm trying to do anything in Eclipse. Just a simple program as this will display a bunch of error messages:

package lab6;

public class Hellomsg {
    System.out.println("Hello.");

}

These are the errors I receive on the same line as I have my

"System.out.println":
"Multiple markers at this line

- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error on token ".", @ expected after this token
- Syntax error, insert "Identifier (" to complete MethodHeaderName"
Ptosis answered 12/10, 2016 at 13:44 Comment(3)
You have a method call outside of a method which is not possiblePricket
And i am sure it also not works before the Windows updatePricket
the code you have mentioned should not have worked ever. So, when you say Eclipse worked fine a couple of days ago, just want to check if you have given a wrong example or?Bornie
J
15

You can't just have statements floating in the middle of classes in Java. You either need to put them in methods:

package lab6;

public class Hellomsg {
    public void myMethod() {
         System.out.println("Hello.");
    }
}

Or in static blocks:

package lab6;

public class Hellomsg {
    static {
         System.out.println("Hello.");
    }
}
Josi answered 12/10, 2016 at 13:46 Comment(3)
Who the hell gave -1 to this answer? It is the best answer hereWittenburg
My god. I had a lot of issues with eclipse before I got this issue. When I solved previous issues I got these error messages because I completely forgot how to write the simplest code. I am very ashamed. Thank you for waking me up.Ptosis
Unfortunately I can't delete this question now so it is now a permanent symbol for me being a complete noob.Ptosis
S
1

You can't have statements outside of initializer blocks or methods.

Try something like this:

public class Hellomsg {
    {
        System.out.println("Hello.");
    }
}

or this

public class Hellomsg {
    public void printMessage(){
        System.out.println("Hello.");
    }
}
Shelbyshelden answered 12/10, 2016 at 13:45 Comment(0)
P
1

You have a method call outside of a method which is not possible.

Correct code Looks like:

public class Hellomsg {
  public static void main(String[] args) { 
    System.out.println("Hello.");
    }
}
Pricket answered 12/10, 2016 at 13:46 Comment(0)
L
0

Just now I too faced the same issue, so I think I can answer this question.

You have to write the code inside the methods not on the class, class are generally used to do some initialization for the variables and writing methods.

So for your issue, I'm just adding your statement inside the main function.

package lab6;
public class Hellomsg {
  public static void main(String args[]){
    System.out.println("Hello.");
  }
}

Execute the above, the code will work now.

Lewan answered 3/1, 2023 at 8:59 Comment(0)
B
0

We get such errors when we write such statement in class only. It will work without error if written inside the public static void main(String ...args){} or inside any method of a class but not merely inside body of a class.

error code

package lab6;

public class Hellomsg {
    System.out.println("Hello.");

working code

package lab6;
public class Hellomsg {
   void printMsg(){
    System.out.println("Hello.");
  }
}
Bloomer answered 3/3 at 5:48 Comment(1)
Pardon me, but I don't see anything in your answer that doesn't already appear in the other answers, so why, after more than seven years, do you decide to add a similar answer to all the others? And for any future answers you may post, you should learn how to use markdown.Herbart

© 2022 - 2024 — McMap. All rights reserved.