Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

inner classes

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys it's me again ,annoying budsy.

I was reading inner classes and i found out that for every outer class variable ,a static function gets defined by the compiler that is responsible for the inner classes having access to outer class variables.Here is an example:

import static java.lang.System.*;

now when we run javap -private A
we see a compiler defined method as:

static boolean access$0(A);

now when the following statement is written:

out.println(beep);

it is converted into

out.println(access$0(implicit reference));

now if the function is static why doesn't the compiler use the classname too since that is how you call the static method that is it should have been :

out.println(A.access$0(implicit reference));
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not Beginning Java material. Moving to Java in General.
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The inner class has access to all methods from its enclosing class without explicitly needing to name that class, except when it leads to ambiguity.
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but the question is that if they both are treated as regular classes by jvm then how does the compiler gets this privilege ? please give further details and thanks
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The inner class instance always contains a reference to the instance of outer class that was used to create the instance of the inner class; this reference is used to call methods and access members of the outer class. You can explicitly access reference to the outer class using OuterClass.this (where OuterClass is the name of the outer class).

There is not any special privilege involved actually. If you implement a static inner class and keep the reference to the outer instance in it, you can implement all of the functionality of non-static inner classes. The built-in solution just lets you do the same with less work and less code.
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but the question is how is the compiler able to execute this :

out.println(access$0(implicit reference));

the access$0(A) is a static method the compiler should execute :
out.println(A.access$0(implicit reference));

how does the compiler know about that method since the classes are regular and compiler uses built in solution ?
 
Die Fledermaus does not fear such a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic