I don't quite understand a "static context" error message

Discussion in 'Java' started by Dan, Aug 22, 2007.

  1. Dan

    Dan Guest

    Here's a slightly altered bit of code from Sun's tutorial:

    package instanceofdemo;
    public class InstanceofDemo {
    public InstanceofDemo() {
    }
    public static void main(String[] args) {
    Parent obj1 = new Parent();
    System.out.println("obj1 instanceof Parent: " + (obj1
    instanceof Parent));
    }

    class Parent{
    }
    } //"Class End Brace"

    The "Parent obj1 = new Parent();" line gets this error:
    non-static variable this cannot be referenced from a static context

    If I move the "Class End Brace" above the "class Parent{" line, the
    error goes away.

    I'm just on the edge of understanding why that should be. Can anyone
    get me the rest of the way there? What's the compiler seeing that
    causes this?
     
    Dan, Aug 22, 2007
    #1
    1. Advertisements

  2. Dan

    Eric Sosman Guest

    Dan wrote On 08/22/07 16:34,:
    As it stands, Parent is an inner class belonging
    to an InstanceofDemo object -- you cannot create a
    Parent in isolation, but only in connection with the
    InstanceofDemo object that owns it. In the static
    method main(), there is no InstanceofDemo object in
    sight, so the compiler complains.

    When you move Parent outside InstanceofDemo, it
    becomes a free-standing class that has no connection
    to InstanceofDemo, and does not need to be associated
    with an InstanceofDemo object.
     
    Eric Sosman, Aug 22, 2007
    #2
    1. Advertisements

  3. Dan () wrote:
    : Here's a slightly altered bit of code from Sun's tutorial:

    : package instanceofdemo;
    : public class InstanceofDemo {
    : public InstanceofDemo() {
    : }
    : public static void main(String[] args) {
    : Parent obj1 = new Parent();
    : System.out.println("obj1 instanceof Parent: " + (obj1
    : instanceof Parent));
    : }

    : class Parent{
    : }
    : } //"Class End Brace"

    : The "Parent obj1 = new Parent();" line gets this error:
    : non-static variable this cannot be referenced from a static context

    : If I move the "Class End Brace" above the "class Parent{" line, the
    : error goes away.

    : I'm just on the edge of understanding why that should be. Can anyone
    : get me the rest of the way there? What's the compiler seeing that
    : causes this?

    Well first, as an aside, I prefer to align my braces vertically, because
    then you can more easily see how the nesting works.

    In your example, the Parent class is _inside_ the InstanceofDemo class.
    Defining a class inside another class is perfectly legit, and is
    conceptually the same as defining anything else inside a class, such as
    member variables and methods.

    Basically, each instance of "InstanceofDemo" has its own version of a
    class called "Parent".

    This would be much clearer if you had a number of different classes, each
    of which defined their own version of a class called "Parent".

    And just like when you access a member variable and need to provide the
    object that contains that instance of the variable, so here, to access the
    "Parent" class, you need to indicate _which_ "Parent" class by providing
    the object that contains that instance of the class definition (so to
    speak).

    When you move the brace then the Parent definition is now outside the
    "InstanceofDemo" class. The code still compiles because either syntax is
    legal.
     
    Malcolm Dew-Jones, Aug 22, 2007
    #3
  4. Dan

    Roedy Green Guest

    see
    http://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF

    In your case moving the brace converts Parent from a free standing
    class to a nested inner instance class that needs an instance of the
    outer class to work. If you had said "static class Parent" it would
    work with brace where it is.
     
    Roedy Green, Aug 23, 2007
    #4
  5. Dan

    Lew Guest

    I've been corrected for this very same misconception.

    Each instance of the outer class does not have its own version of the inner
    class. Each instance of the outer class has the same "version" of, actually
    the same manifestation of the inner class, indeed, the same bytecode in the
    same location in the JVM (assuming no ClassLoader magic). There is only one
    "version" of the inner class.

    However, each instance of the inner class must "belong" to an instance of the
    outer class.
     
    Lew, Aug 23, 2007
    #5
  6. Dan

    Bikal KC Guest

    If u move this last line above "class Parent{"
    then Parent becomes a class of it's own and it compiles
    fine. However, if you do want to include Parent class
    inside InstanceofDemo, use static identifier so that when
    calling from main(...) {...} it works.
    i.e., static class Parent { } call from any _static_
    method like-
    main(...) { Parent p = new Parent(); }


    Cheers
     
    Bikal KC, Aug 23, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.