• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

final keyword Method local inner class

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final keyword in method local inner class:

Is I declare my method-local inner class as final,I cannot instantiate it then how can I access method of it ?

public class HHH
{

public static int sampleMethod()
{
final class AC {
int m(){
return 0;}
}

class CC {
int m() {
return 42;
}
}


AC c = new AC();
// cannot instantiate final class then how can I instantiate in method local //class.Is it valid to do ?
return c.m();

}

public static void main(String[] args) {
System.out.println(sampleMethod());
}
}
[ February 01, 2007: Message edited by: Kasak Tahilramani ]
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There would be no problem if you would have
or
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

AC c = new CC(); is not permitted in your example.

Change it to :
AC c = new AC();

it will then work.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing unusual here. CC is not an AC so you cannot assign it to a reference variable of type AC. CC cannot extend AC because AC is final. This is nothing special to do with method local inner classes.
 
Kasak Tahilramani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank all .But I had edited the post so please correct where I am going wrong.
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you compile and run your code? If not, why not?
If yes, what exactly is your question?

Edit:

// cannot instantiate final class then...


Perhaps you just mixed up "final" and "abstract"?
[ February 01, 2007: Message edited by: Anton Uwe ]
 
Kasak Tahilramani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anton.I tried running my code and it is working fine.Appreciate for your help.

But one question that is bothering me is that,If I cannot instantiate a normal final class, how I am able to do it with method-local class which is final.
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If I cannot instantiate a normal final class, how...


There really is no problem to instantiate a normal final class. You have done it already, for sure, if you ever used String, or Integer, other wrappers, and so on. I still believe you perhaps mix up final and abstract.
 
Not so fast naughty spawn! I want you to know about
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic