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

Inner class having a private constructor

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Trying some code and modifying I cam up with this one...


and this compiled just fine. I was expecting it to show a compiler-error as the private constructor of class B is being called by LineA outside the class.. I was thinking only any code in SectionB, can create an object of class B
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this program

public class Test{
public static void main(String[]args){
Test.A ob = new Test().new A();
System.out.println(ob.i);//Possible--line 1
Super ob1 = new Super();
//System.out.println(ob1.j);Compile time error.--line 2
}
class A{
private int i = 0;
}
}
class Super{
private int j = 23;
}
One object can access a private variable of another object of the same calss. So Line 1 produce no compile time error. But Line 2 produce error.
This is the same case for your program.If your class B not in this class A then this produce compile error.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yes I understand this that an inner class "one" can SEE an inner class "two" of the same encapsulating class.

But i tried a code something like this:

1)I had a class with a non-public-constructor in a package
2)I had another class in a different package

I imported the first class in this code
Yes, it can SEE the first class..

But well as expected I wasn't able to instantiate the first class in this code because it had no public-constructor...

In this case, the class is visible but the constructor is not accessible, rite..

So, in this inner classes code- yes the class is visible,fine. The constructor is also accessible??? unlike the scenario explained above??
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic