• Post Reply Bookmark Topic Watch Topic
  • New Topic

simle question public static variable  RSS feed

 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody please explain me the difference below?


run:
test
0



run:
test
11
 
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
This ...

A Ka wrote:Can somebody please explain me the difference below?




doesn't compile. Java statements, that are not declarations, need to be in a code block -- either initializer, constructor, or method. And please don't run code that doesn't compile -- you won't know what you are getting.

Henry
 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code with a separate statement

won't compile because the separate "n=11" statement is an assignment statement, and you generally can't have an assignment statement outside of any methods. In the other version, the " = 11 " thing is part of the declaration of the "n" variable and that's okay to do.
 
Mannu Khanna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Ka wrote:Can somebody please explain me the difference below?


run:
test
0



Neither will it compile nor there is a way to access the statement n=11;
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!