This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anonymous class example

 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following example, the line: AbstractTest.MyInner minn = new AbstractTest().MyInner() is giving error. It expects ; at the end of this line, but I am trying to instantiate it anonymously and get the value of minn.num. Can anyone explain me how it can be fixed?



Thank you.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is expecting the word "new" before the inner instance...

AbstractTest.MyInner minn = new AbstractTest().new MyInner() {...

However, once that is fixed, you will see that abstract classes (like AbstractTest) cannot be instantiated.
 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps

AbstractTest.MyInner minn = abt.new MyInner();

is what you're looking for?

But besides this, I see that your classes are written as if you expect variables to behave polymorphically -- they do not. You should never redefine a variable in a subclass like this. If the variable needs to have a different value in a subclass, then assign a different value in the constructor of the subclass (or in an anonymous class, in an initialization block). But never try to "override" a variable -- you'll just get weird, hard to understand results.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic