[5338] in java-interest
Identifier re-declaration?
daemon@ATHENA.MIT.EDU (Alfredo Botelho)
Tue Feb 6 09:45:02 1996
From: fred@wipinfo.soft.net (Alfredo Botelho)
To: java-interest@java.sun.com
Date: Thu, 1 Feb 1996 13:43:07 +0500 (GMT+0500)
Hi!
I seem to have a problem with compiling Java code. I haven't read the
Java spec, however I seem to find what seems to be either some inconsistency
in the language or a bug in my compiler (Java for NT).
I was trying to check whether Java permits an identifier to be used as a
variable as well as a method name in the same class.
Below, I have a class Foo, which uses an integer & a method by the name
of foo (note the cases are different).
class Foo {
public int foo = 10;
public int foo() {
return 20;
}
}
Here I have a class Test that instantiates Foo to test it.
class Test {
public static void main(String args[]) {
Foo bar = new Foo();
System.out.println("The variable is " + bar.foo +
" and the method returns " + bar.foo());
}
}
So far the compiler accepts it. On executing, on the VM, I get the
following output:
` The variable is 10 and the method returns 20 '
However, the following rendition of Foo does not compile.
class Foo {
public int foo() {
return 20;
}
/*
* Notice I have the method defined before I declare/ define the
* variable.
*/
public int foo = 10;
}
For this one the compiler spits out -
Foo.java:26: Duplicate variable declaration: int foo was int foo()
public int foo = 10;
^
Foo.java:15: Attempt to reference method foo in class Foo as an instance variable.
System.out.println("The variable is " + bar.foo + " and the method returns " +
^
2 errors
I can't seem to understand what is going on. Can some one explain?
TIA,
'fred.
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com