Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class inside method

 
Aparna Narayanan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If I declare and define a class inside a method, I can access only the final local variables of the method, isn't it? The reason given in RHE for this is that the object of the class declared inside method should be able to outlive the method and this is possible only if variables that can be accessed from the method are final. But then, I am not able to create/access an object of the class declared inside the method outside the method at all!!! Then, what's the big deal in allowing only final variables of the method to be able to be accessed??? Or is there any way that I can create/access an object of the class declared inside a method outside the method??
Thanks,
Aparna
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is a good question. that went through my mind before too. all i know right now is a local class can only have "friendly" access. im fairly sure of this.
 
Aparna Narayanan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randall,
Thanks for ur reply. But then, such a thing is not discussed even in JLS, where else to look for answers? Everywhere, the examples comfortably declare an object inside the method itself. There has been no example/ no mention of how that object can be accessed outside. Any help from anyone would be well appreciated.
Thanks,
Aparna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic