• 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

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

public class A
{
A()
{
class B
{
{
System.out.println("I am in no-arg constructor");
}
}
}

A(int i)
{
class B
{
{
System.out.println("I am in the arg constructor");
}
}
new B();
}
}
class C
{
public static void main(String[] args)
{
A a = new A(1);

}
}
Output:
Two class files corresponding to both inner classes (B) is created in the file system.
The classes compile cleanly and on running C as an application causes "I am in the arg constructor" to be printed on the console
Can anybody explain why inner class names does not clash? How is this mechanism works?
Thanks
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its simple priya just think of constructor as a method and here the scope of class "b" is only inside that method.this class is unknown to the outer world.so it compiles fine just place the first inner class outside and then try it..
asheet
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile it and then look at the names of the class files that it creates. Then all will be revealed.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can understand your confusion on how inner classes of methods are resolved; sometimes simply compiling code and observing the resultant output does not necessarily clear things up for someone. Just remember that scope for inner classes is resolved similarly to other scope situations. The code provided has class B defined twice, each in a different constructor of class A; each of these classes are local to the corresponding constructor methods. Since the constructor is overloaded, when an instance of A, a, is created in main() by passing an int value to the constructor, the second A constructor is the one used, and therefore the "I am in the arg constructor" line is output. There is no conflict between the two B classes since they cannot "see" each other.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The clarification of the confusion by Eric is great. This same question answers 2 questions that are both of my confusion.
Thanks! Eric
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't you think class C should be declared public instead of A?
Rashmi
 
Priya Rajan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rashmi,
while compiling u say javac A.java and when executing u say
java C.
That solves the problem.

Originally posted by Rashmi Hosalli:
don't you think class C should be declared public instead of A?
Rashmi


 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya,
Inner classes are treated as class variables or local variables. So other classes can't see them. However, inner classes treated as class variables can be reached by the name of the outer class then '.' and then the name of the class.Like OuterClass.InnerClass ....
Am i right? If wrong, somebody clarify!
------------------
azaman
[This message has been edited by Ashik uzzaman (edited July 19, 2001).]
[This message has been edited by Ashik uzzaman (edited July 19, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic