Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Exception handling with Anonymous inner class

 
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have  a doubt regarding how .class files are named when we are  dealing with anonymous classes. I know that when we create a  inner class  one  .class file is generated named Outerclassname$InnerClass.class along with the normal .class file
and if we create an anonymous class then the .class file is named OuterClass$1.class

Now check this code :

Please assume I have a Test2 Class which has a method taste();

Now I am creating this code

//
Now my Question is from the above code  4 .class files should be created  as:
1) Test.class (for outer class)
2)Test$1.class(for anonymous class)
3)Test$1$MoreInner.class ( for class created inside anonymous class)
4)Test$Checker.class (for class created inside catch block)

But name of 4th .class file is Test$1Checker.class. Why there is a '1' before inner class name ? why it is Test$1Checker.class instead of Test$Checker.class ?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaishakh Menon wrote:
But name of 4th .class file is Test$1Checker.class. Why there is a '1' before inner class name ? why it is Test$1Checker.class instead of Test$Checker.class ?


Because class 'Checker' is defined inside the anonymous class, and not directly inside class Test.

You should indent your code properly; because of the bad indentation, it is really hard to see the structure of the code.
 
Vaishakh Menon
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the poor indentation.Please check the code below.


 
Marshal
Posts: 74359
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
I think you have misspelt the name of the fourth class. I think the reason you are getting such names would be obvious if you formatted your text properly; the indentation is so bad that nobody can read it. Please post the whole code again, with correct indentation.
 
Campbell Ritchie
Marshal
Posts: 74359
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now you have corrected the indentation (thank you ), you can see that the named class is inside the anonymous class. You can see therefore that its .class file will be named Test$1checker.class, not Test$1Checker.class.
 
Vaishakh Menon
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have already Posted the Code Please Check it out.
My anonymous class is Closed at Line no 20 then how  my checker class  is inside my anonymous class.

Sorry for the indentation.This is my 1st post so not familiar.
 
Vaishakh Menon
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Campbell  the name is Test$1checker.class
If it is inside my anonymous class then why the name is not Test$1$checker.class ?



 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As an FYI ... I don't think the file naming scheme for nested classes are specified in the documentations. In other words, it is an implementation detail, and may change in the future.

So, I don't know if you are going to get any good answers as to "why" something was implemented that way -- unless of course, the designer/implementors from Sun/Oracle shows up at the ranch.

Henry
 
Campbell Ritchie
Marshal
Posts: 74359
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. Are there rules about the naming of anonymous classes and such local classes at all? Have you looked in the Java® Language Specification or the JVM Specification?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic