Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Dan - 22 Aug 2007 21:34 GMT
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?
Eric Sosman - 22 Aug 2007 21:44 GMT
Dan wrote On 08/22/07 16:34,:
> Here's a slightly altered bit of code from Sun's tutorial:
>
[quoted text clipped - 21 lines]
> 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.

Signature

Eric.Sosman@sun.com

Malcolm Dew-Jones - 22 Aug 2007 22:16 GMT
: Here's a slightly altered bit of code from Sun's tutorial:

: package instanceofdemo;
: public class InstanceofDemo {
[quoted text clipped - 5 lines]
: 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.
Lew - 23 Aug 2007 01:53 GMT
> In your example, the Parent class is _inside_ the InstanceofDemo class.  
> Defining a class inside another class is perfectly legit, and is
[quoted text clipped - 3 lines]
> 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.

Signature

Lew

Roedy Green - 23 Aug 2007 00:34 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Bikal KC - 23 Aug 2007 06:14 GMT
> Here's a slightly altered bit of code from Sun's tutorial:
>
[quoted text clipped - 11 lines]
>     }           
> } //"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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.