Y
yeah
Never had much use for defining classes inside methods but this is
something interesting I noticed while playing around with it. Consider
this example:
public class test {
public static void main(String args[]) {
test t = new test() ;
System.out.println( t.doIt() ) ;
}
public static Thing doIt() {
class Thing {
public int result ;
Thing(int v) { result = v ; }
}
Thing t = new Thing(4) ;
//return t ; // compile error
return null ; // compiles fine
}
}
If doIt returns null, the program compiles and outputs "null". If it
returns 't' (the new Thing), the compiler error states:
test.java:24: incompatible types
found : Thing
required: Thing
return t ;
^
1 error
Of course this is reasonable (if perverse) since the method return
type Thing presumably has a different symbol table value than the
method internal Thing class.
What's really strange to me is that this distinction is represented by
the error message. If, for instance, the return type of doIt() is
changed to ThingX, you get something like:
test.java:22: cannot resolve symbol
symbol : class ThingX
location: class A
public ThingX doIt() {
^
1 error
So it looks like the compiler isn't entirely clear on whether or not
it distinguishes between return type Thing and method-local Thing.
something interesting I noticed while playing around with it. Consider
this example:
public class test {
public static void main(String args[]) {
test t = new test() ;
System.out.println( t.doIt() ) ;
}
public static Thing doIt() {
class Thing {
public int result ;
Thing(int v) { result = v ; }
}
Thing t = new Thing(4) ;
//return t ; // compile error
return null ; // compiles fine
}
}
If doIt returns null, the program compiles and outputs "null". If it
returns 't' (the new Thing), the compiler error states:
test.java:24: incompatible types
found : Thing
required: Thing
return t ;
^
1 error
Of course this is reasonable (if perverse) since the method return
type Thing presumably has a different symbol table value than the
method internal Thing class.
What's really strange to me is that this distinction is represented by
the error message. If, for instance, the return type of doIt() is
changed to ThingX, you get something like:
test.java:22: cannot resolve symbol
symbol : class ThingX
location: class A
public ThingX doIt() {
^
1 error
So it looks like the compiler isn't entirely clear on whether or not
it distinguishes between return type Thing and method-local Thing.