example to illustrate :
public class Something
{
private static int number;
static Something()
{
int number = 10;
// Syntax to distingish between local variable and static variable ?
}
}
Inside the static constructor, is there a syntax that can be used to distinguish between the local variable called "number", and the static variable of the same name ?
Neither of the local static int sVar; will be allocated on the stack (if there is such a concept at all), each function body is a separate block, which limits the scope of the local static variable. The compiler uses a renaming scheme to ensure these variables are distinct at link time.
In Java, a static variable is a class variable (for whole class). So if we have static local variable (a variable with scope limited to function), it violates the purpose of static. Hence compiler does not allow static local variable.
Although it is usually a bad idea, you can declare a formal parameter or a local variable with the same name as one of the instance variables.
Example. Static variables have a lifetime that lasts until the end of the program. If they are local variables, then their value persists when execution leaves their scope.
Something.number
Obvious, no?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With