• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class reference

 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MyOuter {
public static class MyInner {
public MyInner() {
System.out.println("Test");
}
}

public static void main(String[] args) {
MyInner i1=new MyInner(); //line a
MyOuter.MyInner i2=new MyInner(); //line b
MyOuter.MyInner i3=new MyOuter.MyInner(); //line c
}
}
The program compile and run. So line a, b, c are all legal ways to create instance of static member class MyInner,right?
In fact, if the main method is replaced by the following one, it can still compile and run.
public static void main(String[] args) {
MyOuter o=new MyOuter();
MyOuter.MyInner i=o.new MyInner();
}
My question is why MyOuter.MyInner i=new MyOuter().new MyInner();
compile but throw a run time exception?
 
Rajesh Patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bin,
MyOuter.MyInner i=new MyOuter().new MyInner();
does not throw a run time exception.
I tried it and it runs ok.

------------------
rptl
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajesh,
I still got the exception :-(
Exception in thread "main" java.lang.VerifyError......
Have any idea about this?
 
mousami bhattacharya
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me too ,I am getting the error
Exception in thread "main" java.lang.VerifyError: (class: Outer, method: main si
gnature: ([Ljava/lang/String V) Expecting to find unitialized object on stack
Is it again one of the version problems ??? I am using jdk 1.3
any help ???
Mousami
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still got the same exception today. My java version is 1.2.2 and I'm using Windows NT workstation 4.0.
Can anybody help?
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bin!!!
There is no version problem. Only satck problem because in one statement you first create outer's object and then Inner's object. Remind it that for more than one processes in one statement JVM creates a stack. So all the processes push into stack but performed after all statement read. So "new Myouter()" is in stack not executed so how can you do "new MyOuter().new MyInner()". I think this is clear now. Tell me ranchers if i wrong.
Farhan
[This message has been edited by Farhan Tariq (edited May 25, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic