Results 1 to 2 of 2
  1. #1
    phobosforever is offline Member
    Join Date
    Jul 2011
    Rep Power

    Default Static Block and variable problem

    Here is a code that I encountered while working on the Eclipse IDE.

    class Demo
    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?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    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 for the precise rules governing forward reference to class variables.

Similar Threads

  1. Replies: 5
    Last Post: 12-01-2010, 07:43 AM
  2. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  3. Replies: 3
    Last Post: 02-11-2010, 10:59 AM
  4. [SOLVED] static block or constructor?
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 05-27-2008, 02:21 PM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts