wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class" Watch "Inner class" New topic
Author

Inner class

Priya Rajan
Greenhorn

Joined: Jul 06, 2001
Posts: 27

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

Joined: Nov 15, 2000
Posts: 83
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

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 08, 2001
Posts: 44
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

Joined: Jun 25, 2001
Posts: 50
don't you think class C should be declared public instead of A?
Rashmi
Priya Rajan
Greenhorn

Joined: Jul 06, 2001
Posts: 27
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

Joined: Jul 05, 2001
Posts: 2370

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).]


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class
 
Similar Threads
constructors with inner classes.
why compile the file there are two class file was created?
A Gud Question on Inner class
Constructor problem
anglefire.com question