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. Advertising

  2. Dan

    Eric Sosman Guest

    Dan wrote On 08/22/07 16:34,:
    > 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?


    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. Advertising

  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

    On Wed, 22 Aug 2007 13:34:28 -0700, Dan <> wrote,
    quoted or indirectly quoted someone who said :

    >non-static variable this cannot be referenced from a static context


    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 Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 23, 2007
    #4
  5. Dan

    Lew Guest

    Malcolm Dew-Jones wrote:
    > 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".


    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
    Lew, Aug 23, 2007
    #5
  6. Dan

    Bikal KC Guest

    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"


    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon

    Error message I don't understand

    simon, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    300
    Hatim R
    Dec 5, 2003
  2. OscarC
    Replies:
    7
    Views:
    843
    Roedy Green
    May 9, 2004
  3. Patrick M.

    I don't quite understand this exercise...

    Patrick M., Sep 26, 2005, in forum: C Programming
    Replies:
    10
    Views:
    587
    Keith Thompson
    Sep 26, 2005
  4. gwellman
    Replies:
    3
    Views:
    135
    gwellman
    Dec 4, 2003
  5. Jedrin
    Replies:
    1
    Views:
    510
    sreservoir
    Feb 18, 2012
Loading...

Share This Page