Anonymous Class question

D

duff

Hi,

Can someone pls explain why the error in the example below? tia.


public class testclass {

public static void main (String[] a) {

testclass tc = new testclass() {
int intvar = 0;
intvar = 1; // compile time error here - why?
};

}

}
 
S

Stefan Ram

duff said:
new testclass() {
intvar = 1; // compile time error here - why?
}

This is supposed to be a class instance creation expression

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9

so

{ intvar = 1; }

needs to be a class body, which only may contain
ClassBodyDeclarations.

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.6

"intvar = 1;" is not such a ClassBodyDeclaration.

A ClassBodyDeclaration is one of

InstanceInitializer
StaticInitializer
ConstructorDeclaration
FieldDeclaration
MethodDeclaration
ClassDeclaration
InterfaceDeclaration
;
 
M

Mike Schilling

duff said:
Hi,

Can someone pls explain why the error in the example below? tia.


public class testclass {

public static void main (String[] a) {

testclass tc = new testclass() {
int intvar = 0;

This declares the field intvar (and initializes it.)
intvar = 1; // compile time error here - why?

This is a statement that needs to be inside a method, but isn't.

There's nothing special about anonymous classes here; it's the same error as
in

class HasError
{
int intvar = 0;
intvar = 1; // needs to be inside some method
}
 
D

duff

Thank you all for your replies.

I felt silly after reading your replies. Never used anonymous inner
class - as you can tell :(

..

duff said:
Hi,

Can someone pls explain why the error in the example below? tia.


public class testclass {

public static void main (String[] a) {

testclass tc = new testclass() {
int intvar = 0;

This declares the field intvar (and initializes it.)
intvar = 1; // compile time error here - why?

This is a statement that needs to be inside a method, but isn't.

There's nothing special about anonymous classes here; it's the same error as
in

class HasError
{
int intvar = 0;
intvar = 1; // needs to be inside some method
}
 
H

Hiran Chaudhuri

duff said:
Hi,

Can someone pls explain why the error in the example below? tia.


public class testclass {

public static void main (String[] a) {

testclass tc = new testclass() {
int intvar = 0;
intvar = 1; // compile time error here - why?
};
}
}

Have a look at the structure of testclass. It declares one field variable
intvar, with the value 0 being assigned by the default constructor
implicitly. This line is ok.

But the assignment intvar=1 is neither a new field declaration, nor is the
code inside a constructor or method. Therefore the compiler fails.

Hiran Chaudhuri
 
D

duff

Hi,

Can someone please explain why the code below works? in particular
printing of the "memberInt" variable within instant initializer of the
anonymous class.

The anonymous class is within a static context but yet is accessing a
non static variable - confused :(

Possible theory; inner classes has implicit ref to enclosing class?
but we are in a static context?

public class TestAnony {
int memberInt = 123;

public static void main (String a[]) {

new TestAnony() { // anonymouse class declaration

{ // instant initializer
System.err.println(memberInt); // Why is this OK?
}

};

class SomeLocal { // local class declaration
public SomeLocal() { // constructor
//System.err.println(memberInt); //static Ctx. this won't work
}
}


} // end of main method

}

tia
 
T

Thomas Hawtin

duff said:
public class TestAnony {
int memberInt = 123;

memberInt is an instance member of TestAnnoy with the default, package
private access.
public static void main (String a[]) {

new TestAnony() { // anonymouse class declaration

{ // instant initializer

An anonymous nested class, so no outer this. However it does extend the
TestAnnoy class and is clearly in the same package, so has access to the
public, protected and package private (like memberInt) variables of the
superclass.
System.err.println(memberInt); // Why is this OK?
}

};

class SomeLocal { // local class declaration
public SomeLocal() { // constructor

If you did this from a non-static method, you would rather confusingly
have two memberInt variables. Not good.
//System.err.println(memberInt); //static Ctx. this won't work
}
}


} // end of main method

}

It's probably best to avoid any even slightly confusing inheritance when
dealing with nested classes.

Tom Hawtin
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top