cawe011
cawe011

Reputation: 69

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

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"

Upvotes: 5

Views: 45907

Answers (5)

Ravi Singh
Ravi Singh

Reputation: 1

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.");
  }
}

Upvotes: 0

Theri Muthu Selvam
Theri Muthu Selvam

Reputation: 162

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.

Upvotes: 0

Jens
Jens

Reputation: 69450

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.");
    }
}

Upvotes: 1

Mureinik
Mureinik

Reputation: 311508

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.");
    }
}

Upvotes: 15

Adam Arold
Adam Arold

Reputation: 30538

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.");
    }
}

Upvotes: 1

Related Questions