Forums Register Login

Problem running Inner Class Applications...

+Pie Number of slices to send: Send
Hi All,

I have just started learning about inner classes. To observe the effect of inner classes I tried to run the following application taken from a book. It compiled fine but gave a huge runtime error. The program is as below:



class InnerTest{
public static void main(String[] args){
Outer o = new Outer();
Outer.Inner i = o.new Inner();//one way
i.seeOuter();
i = new Outer().new Inner();//another way
i.seeOuter();
}
}
public class Outer{
private String s = "outer s";
void makeAndSeeInner(){
System.out.println(this);//refers to Outer.this
Inner i = new Inner();//No need of Outer.this explicitly, because, Outer.this already exists here.
i.seeOuter();
}
void seeInner(){
System.out.println(s);//How to see Inner s here? You can't, because Inner.this not present.
}
strictfp class Inner{
private String s = "inner s";
void seeOuter(){
System.out.println(this);
System.out.println(Outer.this.s);//Need to mention Outer because this refers to Inner.this here.
System.out.println(s);//Or, this.s
}
}
abstract class AbInner{
private String s = "abstract inner s";
void seeOuter(){
System.out.println(this);//this refers to the subclass not the abstract class.
System.out.println(Outer.this.s);
System.out.println(s);
}
abstract void abMethod();
}
class Some extends AbInner implements Runnable, Animal{//can extend and implement
public void run(){}
void abMethod(){
System.out.println(this);
System.out.println(super.s);
}
}
public static void main(String[] args){
Inner i = new Outer().new Inner();
//Inner i = new Inner();//can't exist w/o outer class instance
i.seeOuter();
Outer o = new Outer();
o.makeAndSeeInner();
o.seeInner();
//new Outer().makeAndSeeInner();
Some so = new Outer().new Some();
so.seeOuter();
so.abMethod();
}
}
interface Animal{
}




The runtime exception that i received was:



Exception in thread "main" NoClassDefFoundException : Outer
caused by java.lang.ClassNotFoundException : Outer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrevileged(Native Method)
at java.lang.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)


I got similar runtime exception for other two Inner Class Apps also that i tried to run.Please Help!!!


Regards,

Rekha
+Pie Number of slices to send: Send
It ran perfectly well when I tried it.
Please use code tags on your quoted code.
Maybe you forgot to compile all the classes?
+Pie Number of slices to send: Send
Hi.

This code is stored under the name Outer.java on my system.
I compiled it : javac Outer.java
It compiled fine and I checked that InnerTest.class, Outer.class, Outer$Inner.class, Outer$AbInner.class and Outer$Some.class were present in the directory after compilation. Then which class file is it looking for..?

Regards
Rekha
[ March 13, 2008: Message edited by: Rekha Anand ]
+Pie Number of slices to send: Send
Don't know, I am afraid.
+Pie Number of slices to send: Send
Somebody Please Help !!!
+Pie Number of slices to send: Send
Less of the thank you; we are trying to help.

Which version of jdk are you using? Have you done an "ls" (Unix/Linux) or "dir" (DOS/Windows) call while you are in the terminal window? Have you got a . for current directory in your CLASSPATH?

I have just tried compiling it and it wouldn't compile InnerTest, presumably because of the file name (earlier I copies InnerTest into its own file). It would however compile and run Outer. It would appear you can't find Outer.

I suspect you have either slipped off the directory you think you are in, or you have the . missing from your CLASSPATH.
+Pie Number of slices to send: Send
. . . and here is a copy-and-paste from my terminal window

[Campbell@queeg ~]$ cd java/applications
[Campbell@queeg applications]$ gedit Outer.java&
[1] 3157
[Campbell@queeg applications]$ javac Outer.java
[Campbell@queeg applications]$ java Outer
Outer$Inner@3e25a5
outer s
inner s
Outer@19821f
Outer$Inner@addbf1
outer s
inner s
outer s
Outer$Some@a90653
outer s
abstract inner s
Outer$Some@a90653
abstract inner s
[Campbell@queeg applications]$ javac InnerTest.java
javac: file not found: InnerTest.java
Usage: javac <options> <source files>
use -help for a list of possible options
[Campbell@queeg applications]$ ls Outer*
Outer$AbInner.class Outer$Inner.class Outer$Some.class
Outer.class Outer.java
[Campbell@queeg applications]$

 
+Pie Number of slices to send: Send
Sir,
I am using jdk1.6.0_05 downloaded from sun. My classpath is properly set with dot.And I am running it from the proper dir.

Regards
Rekha
[ March 13, 2008: Message edited by: Rekha Anand ]
+Pie Number of slices to send: Send
Don't know, I am afraid. As I said, I have compiled and run your code twice on different computers.
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2163 times.
Similar Threads
Interface - AbstractClass Collision
How can acces inner class content from outer class if inner class is abstract?
Nested classes - Series of doubts
Inner classes
Inner class references (this & super)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:03:09.