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
: 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
>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
> 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