• Post Reply Bookmark Topic Watch Topic
  • New Topic

explain me what is the wrong in this code  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think something is wrong? (and please UseCodeTags)

Hint: yes, something is wrong, but please tell us exactly what errors you are seeing.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y S.O.P cant be used in line 3
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly - because you cannot put arbitrary statements like System.out.println(...) at class level, outside of a method.

At class level, you can only have declarations for member variables, optionally with an initialization expression.
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to the OP's post. Please read UseCodeTags to learn how to use them.

See how much better it looks!
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may i know the reason behind it!!!
 
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
bairava surya wrote:may i know the reason behind it!!!


Simply. The language specification never defined that syntax.... which leads to the next question... Assuming the syntax is legal, what do you expect to happen? Since you seem shocked that it is not allowed, we can only assume that you have an expectation of a result?

Henry
 
Alex Hintz
Greenhorn
Posts: 7
1
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I just started to learn java (actually programming in general) yesterday, so please forgive me/correct me if I am wrong on any of this.

In your example you create the Ennum class and then in main() you create the new Ennum object k. The Ennum class doesn't know what to do with System.out.println("hi"); Also the Object k is created but never told to do anything other then exist. Below I assigned the "hi" to a string variable that could be called after the object was created.

Please forgive me if I am way off base (especially with my explanation).


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Alex, welcome to the Ranch and your explanation is correct.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!