Variable scoping in Kaffe and JDK

Bill Huey billh at mag.ucsd.edu
Mon Mar 1 01:27:14 PST 1999


> switch(vegetable) {
> 
>    default:
>       int fruit = 1;
>       .
>       .
>       .
>       break;
> 
>    case 1:
>       int fruit = 2;
>       .
>       .
>       .
>       break;
> }
> 
> Kaffe does not complain about this, however Sun's javac
> compiler does complain with a message that says "fruit"
> is already declared in the _method_. Well, "fruit" in
> my code was not declared anywhere else in the function
> except in with the case blocks within the switch block.
> 
> So, which way is the real way? I'm curious to know since,
> I lost twenty points on my Data Structures homework. :)

I'm almost certain the the Sun method is the correct method,
because variables are defined on a block basis "{}".

The "{}" generally define a particular scope.

In the C++ language specification there was a similar problem
to what you're encountering now.

===

for(int i; i < number; ++i)
{
	int i = something;
}

The above variable declarations are ambiguous and I'm not sure
if the C++ commitee ever resolved that.

There are people that know much more about this than I do.

bill

> John B. Lee, Undergraduate
> Social and Decision Science; Carnegie Mellon University
> jbl at andrew.cmu.edu



More information about the kaffe mailing list