• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class

 
Priya Rajan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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.
 
JOSEPH BIH
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic