• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is this code showing compile time error?

 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,





it is showing a compile time here...but i am not able to understand why it is showing an error....

if i remove this line then this code shows the output as:

Output:-2

regards
Sumit
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message that the compiler provides is quite helpful in determining the cause. In this case, it says, "illegal forward reference" at that line. So that means you are trying to use a variable before it has been textually declared. This section from JLS should be helpful in understanding why.

HTH,
Paul.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can correct the problem by declaring x before referring to it
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you put the line



above your static block instead of below, it will compile.

The compiler will run through all that static stuff outside of methods first and do it sequentially.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim McGuire wrote:The compiler will run through all that static stuff outside of methods first and do it sequentially.

But that doesn't explain why there is no compiler error when you delete line 5 even though x is still referenced on line 4 before it is declared on line 7
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Tim McGuire wrote:The compiler will run through all that static stuff outside of methods first and do it sequentially.

But that doesn't explain why there is no compiler error when you delete line 5 even though x is still referenced on line 4 before it is declared on line 7


you are right, it does not explain why. Reading the link to the JLS:



The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
The usage is not on the left hand side of an assignment.
The usage is via a simple name.
C is the innermost class or interface enclosing the usage.

A compile-time error occurs if any of the four requirements above are not met.


the statement x = 2 does meets all the above conditions. System.out.println(x) must violate the fourth condition. this usage is enclosed in another class.
 
Alex Theedom
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The declaration of a member needs to appear textually before it is used therefore if you remove line 5 (System.out.println(x);) the variable is not being used before it is declared on line 7.
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static blocks can be put similar to the below snippet:



Hence compilation error.

Larsen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic