File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes two inner classes with the same name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "two inner classes with the same name" Watch "two inner classes with the same name" New topic
Author

two inner classes with the same name

Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
Posts: 57

The output:
I am in the arg constructor
why the line-1 is not executed. why it does not create a new object of B

if i uncomment the line //A aa = new A();
output is:
I am in the arg constructor
I am in no-arg constructor

Can anyone explain the reason why?
Pratyush Verma
Greenhorn

Joined: Oct 08, 2012
Posts: 2

I am in the arg constructor
why the line-1 is not executed. why it does not create a new object of B

Line 1 wont be executed in first case because in A a = new A(1); call to A(int i) constructor is made and the instance of inner class B inside A(int i) is created.

if i uncomment the line //A aa = new A();
output is:
I am in the arg constructor
I am in no-arg constructor


That's because call to A() constructor is made and in this case line 1 is also executed to create an instance of your inner class inside A() constructor.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch Pratyush Verma
Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
Posts: 57
Pratyush Verma wrote:
I am in the arg constructor
why the line-1 is not executed. why it does not create a new object of B

Line 1 wont be executed in first case because in A a = new A(1); call to A(int i) constructor is made and the instance of inner class B inside A(int i) is created.
if i try to create instance of class B in constructor A() (shown below), still it does not print the "I am in no-arg constructor"
A()
{
class B
{
{ System.out.println("I am in no-arg constructor"); }
}
new B();
}
if i uncomment the line //A aa = new A();
output is:
I am in the arg constructor
I am in no-arg constructor


That's because call to A() constructor is made and in this case line 1 is also executed to create an instance of your inner class inside A() constructor.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ilakya, just for clarity , you cant have two inner classes with same name, because of name collision in the same scope.
but you can have two different *method local inner classes* with same name in different block since both live in their own scope.
Pratyush Verma
Greenhorn

Joined: Oct 08, 2012
Posts: 2

Seetharaman Venkatasamy wrote:Welcome to CodeRanch Pratyush Verma

Thank you Seetharaman Venkatasamy.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: two inner classes with the same name