Results 1 to 2 of 2
- 07-04-2011, 12:55 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 1
- Rep Power
- 0
Static Block and variable problem
Here is a code that I encountered while working on the Eclipse IDE.
class Demo
{
static
{
x = 100;
System.out.println(x);
}
public static void main(String args[])
{
//System.out.println(x);
}
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
Classes
dbThe 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 8.3.2.3 for the precise rules governing forward reference to class variables.
Similar Threads
-
non-static variable cannot be referenced from static context...
By MadJack in forum New To JavaReplies: 5Last Post: 12-01-2010, 06:43 AM -
non-static variable cannot be referenced from a static context
By keo in forum New To JavaReplies: 5Last Post: 10-15-2010, 04:21 AM -
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:59 AM -
[SOLVED] static block or constructor?
By JT4NK3D in forum New To JavaReplies: 3Last Post: 05-27-2008, 02:21 PM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks