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

D

Dan

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?
 
E

Eric Sosman

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

Malcolm Dew-Jones

Dan ([email protected]) 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.
 
L

Lew

Malcolm said:
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.
 
B

Bikal KC

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top