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 / January 2006

Tip: Looking for answers? Try searching our database.

Anonymous Class question

Thread view: 
duff - 15 Jan 2006 03:49 GMT
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?
   };

 }

}
Stefan Ram - 15 Jan 2006 03:56 GMT
>    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
;       
Mike Schilling - 15 Jan 2006 16:36 GMT
> Hi,
>
[quoted text clipped - 6 lines]
>    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
   }
duff - 15 Jan 2006 19:44 GMT
Thank you all for your replies.

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

.

>> Hi,
>>
[quoted text clipped - 21 lines]
>        intvar = 1;            // needs to be inside some method
>    }
Hiran Chaudhuri - 17 Jan 2006 09:00 GMT
> Hi,
>
[quoted text clipped - 10 lines]
>  }
> }

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
duff - 17 Jan 2006 22:59 GMT
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
Thomas Hawtin - 18 Jan 2006 07:41 GMT
> 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?
>       }
[quoted text clipped - 3 lines]
>     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
>       }
[quoted text clipped - 3 lines]
>
> }

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

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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



©2008 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.