Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A Gud Question on Inner class

 
Rajesh MadhanGopal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Anil Kumar Saha
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is running.
I'm getting the output:-

inside b's arg constructor
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is giving an exception then what exception is it giving?
"a run time exception" is no information.
 
Rushikesh Wagh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abhishek Pratap
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alok Prax
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic