• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Sun Guoqiao's Mock Exam 2 Quest. 9

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sun Guoqiao's Mock Exam No.2 ex. no. 9
public class Test009
{
public static void main(String args[])
{
Test009.Inner inner1=new Test009().new Inner(); //1
Inner inner2=new Test009().new Inner(); //2
inner2.method(); //3
Noninner ninr=new Noninner();// This has been added by me
//ninr.methodA(); //4 This has been added by me
}
class Inner
{
private void method()
{
System.out.println("0");
}
}
}
// This portion has been added by me
class Noninner{
private void methodA()
{
System.out.println("10");
}
}
____________________________________________________________________
My Doubt:-
The method invocation at //3 works fine even though it calls a private method of inner class in other class (Here it is the enclosing outer class.).
The method invocation at //4 (commented out) causes the expected compile time error due to invocation of private method in other class.
Why this ambiguity? Does it mean that any method( having any access modifier), defined within an inner class is also visible in the Outer/enclosing class?
Please Clarify?
Thanks in advance,
Raj Shah
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raj,
According to JLS 6.6.1,
a private member of a class is accessible if it is accessed
from within the body of the top-level class that encloses the
declaration of the member.
So, //3 is allowed while //4 is not allowed.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
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