Results 1 to 2 of 2
- 07-04-2011, 12:55 PM #1Member
- Join Date
- Jul 2011
- Rep Power
Static Block and variable problem
Here is a code that I encountered while working on the Eclipse IDE.
x = 100;
public static void main(String args)
static int x =1000;
Inside the static block, at the prin statement, it gives me an error "Cannot reference a field before it is defined", whereas, if I comment that, and uncomment the print statement inside main, it works fine. What seems to be the problem here?
- 07-04-2011, 01:28 PM #2
The static initializers and class variable initializers are executed in textual order.
Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §18.104.22.168 for the precise rules governing forward reference to class variables.
- By MadJack in forum New To JavaReplies: 5Last Post: 12-01-2010, 07:43 AM
- By keo in forum New To JavaReplies: 5Last Post: 10-15-2010, 04:21 AM
- By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 10:59 AM
- By JT4NK3D in forum New To JavaReplies: 3Last Post: 05-27-2008, 02:21 PM
- By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM