javac does not recognize the inner class that is within a method within if_else, for or while statements in that same method.
The "Java Tutorial Second edition" page 156 explicitly indicates that it should work.
Test1.java demonstrates the error failure with JDK 1.1.6 and JDK1.1.7. It compiles with NO error on JDK1.2 beta4. Test2.java and Test3.java are the workaround implementations.
This is Test1.java...
import java.awt.*;
import java.util.*;
public class Test1 {
public Image test1 ()
{
class MyImage {
Image image;
long expiration;
}
MyImage mi;
if ( (new Date().getTime()) > 0 ) {
return (null);
} else {
mi = new MyImage();
mi.image = null;
}
return(mi.image);
}
}
Test1.java does not compile, fails with the error
Test1.java:16: Class MyImage not found in type declaration.
mi = new MyImage();
^
Test2.java:
It compiles ok when we move the inner class that was within a method
to just being an inner class within a class.
import java.awt.*;
import java.util.*;
public class Test2 {
class MyImage {
Image image;
long expiration;
}
public Image test1 ()
{
MyImage mi;
if ( (new Date().getTime()) > 0 ) {
mi = new MyImage();
mi.image = null;
} else {
mi = new MyImage();
mi.image = null;
}
return(mi.image);
}
}
Test3.java:
It also compiles ok when we don't refer to the inner class that is
within a method from an if_else, for or while statement.
import java.awt.*;
import java.util.*;
public class Test3 {
public Image test1 ()
{
class MyImage {
Image image;
long expiration;
}
MyImage mi;
if ( (new Date().getTime()) > 0 ) {
return(null);
}
mi = new MyImage();
mi.image = null;
return(mi.image);
}
}
sandhya.vora@Eng 1998-09-30
I tested Test1.java with the /usr/local/java/JDK1.1.7/solaris. and the code still fails with the error as mentioned in the evalution section. Which "FCS version of the compiler" has been used in your testing?
My configuration is as below:
SunOS vora 5.5.1 Generic sun4u sparc SUNW,Ultra-1
setenv CLASSPATH .:/home/sandhya:/usr/local/java/jdk1.1.7/solaris/lib/classes.zip
setenv PATH /usr/local/java/jdk1.1.7/solaris/bin:${PATH}:.
The output of the "which" commands are as below:
vora% which javac
/usr/local/java/jdk1.1.7/solaris/bin/javac
vora% which java
/usr/local/java/jdk1.1.7/solaris/bin/java