my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is this code showing compile time error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is this code showing compile time error?" Watch "why is this code showing compile time error?" New topic
Author

why is this code showing compile time error?

Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

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

Joined: Sep 23, 2000
Posts: 3253
    
    2
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

You can correct the problem by declaring x before referring to it


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

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

Joined: Aug 05, 2005
Posts: 3420
    
  12
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


Joanne
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

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
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

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.


Alex Theedom - Senior Java EE Developer (OCPJP/OCPJWCD)
Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
The static blocks can be put similar to the below snippet:



Hence compilation error.

Larsen.
 
 
subject: why is this code showing compile time error?
 
Similar Threads
What will be the output
Shift operator
simple prog.
Not operator
abstract class instantiation