aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class reference 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 "inner class reference" Watch "inner class reference" New topic
Author

inner class reference

Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
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

Joined: May 21, 2001
Posts: 5
Hi Bin,
MyOuter.MyInner i=new MyOuter().new MyInner();
does not throw a run time exception.
I tried it and it runs ok.

------------------
rptl


rptl
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
Hi Rajesh,
I still got the exception :-(
Exception in thread "main" java.lang.VerifyError......
Have any idea about this?
mousami bhattacharya
Ranch Hand

Joined: May 13, 2001
Posts: 40
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

Joined: May 01, 2001
Posts: 82
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

Joined: May 14, 2001
Posts: 54
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner class reference