• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in identifier  RSS feed

 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

below code give a identifier expected error can any one tell me what is the extract reason for that?..


 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is line 4. You can't have arbitrary code (like that assignment) in the class body. A class body can only have variable declarations (like line 3), method, or inner class declarations, and instance and static initializer blocks. Assignments (and switches, and loops, and if/else statements, and method calls) must be used in a code block (like a method or initializer blocks) unless they are part of a variable declaration (like line 3).
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Steve Luke...

so my line 4 must need to put inside

* static blocks
* init blocks
* Method
* if / else
* switch
* for loop
* inner class

or anything inside a code block. am i correct?..
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randika Isuru wrote:Thank you very much Steve Luke...

so my line 4 must need to put inside

* static blocks
* init blocks
* Method
* if / else
* switch
* for loop
* inner class

or anything inside a code block. am i correct?..



At a class level, code is either a declaration, or statements that must be in a constructor, method, or initializer. That's it. Everything else that you named are either variants of these, or statements that have to be placed in these.

Henry
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for helping me...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!