aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A Gud Question on Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A Gud Question on Inner class" Watch "A Gud Question on Inner class" New topic
Author

A Gud Question on Inner class

Rajesh MadhanGopal
Ranch Hand

Joined: Jan 19, 2006
Posts: 69
Could some one tell me Why it gives a run time exception....



public class a{

a(){
class b{
b(){
System.out.println("inside b's constructor");
}
}
}

a(int i){
class b{
b(){
System.out.println("inside b's arg constructor");
}

}
new b();
}
}

class cc{
public static void main(String args[]){
a aa= new a(5);
}
}


Thanks<br />Rajesh
Anil Kumar Saha
Ranch Hand

Joined: Apr 07, 2004
Posts: 111
it is running.
I'm getting the output:-

inside b's arg constructor


Regards,

Anil Kumar Saha
SCJP 1.4
http://www.agilej.blogspot.com/
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If it is giving an exception then what exception is it giving?
"a run time exception" is no information.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Rushikesh Wagh
Ranch Hand

Joined: Jun 18, 2005
Posts: 58
Its Natural that the above program is giving excpetion.. (java.lang.NoSuchMethodError: main) as in the program class a is public. and class cc is having default modifier.

And we have written main method in a. Thats why the exception is thrown.
As we says $>java a, compiler searches for main method in a.
if we call the same $>java cc It gives result as inside b's arg constructor

Its all about access modifier

Just Modify the code & it works fine...

class a{

a(){
class b{
b(){
System.out.println("inside b's constructor");
}
}
}

a(int i){
class b{
b(){
System.out.println("inside b's arg constructor");
}
}
new b();
}
}

public class cc{
public static void main(String args[]){
a aa= new a(5);
}
}

By the wag good question


Best Regards,
Rushikesh Wagh
SCJP 1.4 (2005)
SCJP 1.6 (2010)
Abhishek Pratap
Ranch Hand

Joined: Apr 14, 2006
Posts: 43
Hi,
if u r compiling this in a same file than please give cc as public class.
thats the only problem in above code.
rest id fine..
it will give output.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
It makes no difference that cc is not public, but a is, except that the file must be named a.java.

.class files will be created for both a and cc.

You have to interpret cc
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Hi friends:
While running the Rajesh's code I am also getting the output as "inside b's arg constructor" But what about new B() statement inside A(int i) constructor??? Why it's not again going inside B's constructor?? I am marking that line as //2 in my code.

Also,when I am trying to run it without any parameter...it's not printing anything. Why it's not printing "inside b's constructor" ???Is it not going inside A() followed by B()???


class A{

A(){
class B{
B(){
System.out.println("inside b's constructor");
}
}
}

A(int i){
class B{
B(){
System.out.println("inside b's arg constructor");
}

}
new B(); //2
}
}

public class C{
public static void main(String args[]){
A aa= new A();
}
}

Am I missing something here??


Regards
Swapnil
[ June 27, 2006: Message edited by: Swapnil Trivedi ]

SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
Alok Prax
Greenhorn

Joined: Jun 28, 2006
Posts: 2
Originally posted by Swapnil Trivedi:
Hi friends:
While running the Rajesh's code I am also getting the output as "inside b's arg constructor" But what about new B() statement inside A(int i) constructor??? Why it's not again going inside B's constructor?? I am marking that line as //2 in my code.


It is going inside B's constructor from line marked as //2... In fact this is the only line of code causing the B's constructor to be called...

Originally posted by Swapnil Trivedi:

Also,when I am trying to run it without any parameter...it's not printing anything. Why it's not printing "inside b's constructor" ???Is it not going inside A() followed by B()???


class A{

A(){
class B{
B(){
System.out.println("inside b's constructor");
}
}
}

A(int i){
class B{
B(){
System.out.println("inside b's arg constructor");
}

}
new B(); //2
}
}

public class C{
public static void main(String args[]){
A aa= new A();
}
}

Am I missing something here??


Regards
Swapnil

[ June 27, 2006: Message edited by: Swapnil Trivedi ]



In this case B would never be instanciated so the control would not go inside B's constructor.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A Gud Question on Inner class