3

According to the doc Local variables in java are declared in methods, constructors, or blocks.

In the below Class A isn't x a local variable too since it is in blocks({}) i know they are called as instance variable but i am confused? If yes Access modifiers cannot be used for local variables but i am sure i can add public private protected ? It also says that local variable are stored in stack but as per the below code x will be stored in heap right since they are part of the instance?

class A{

private int x = 5; // Isn't this a local varibale too since it is in blocks 

}

.

class A{

public void function(){
int x = 5; // this is a local variable since it is declared in a function
private int x2=5; // Error Access modifiers cannot be used for local variables
}

}
1
  • Variables declared inside method definitions are local variables. Since the first example doesn't have any method definitions whatsoever, it's easy to see that it's not a local variable. Commented Apr 1, 2014 at 8:19

4 Answers 4

2

In the below Class A isn't x a local variable too since it is in blocks({})

No. It's not in a block. It's in a class declaration, but that's not a block as such. "Block" isn't synonymous with "text in braces".

To be a bit clearer, local variables are declared in:

If you look at the production for a class declaration, that's not a Block (unlike the production for static initializers and instance initializers).

Sign up to request clarification or add additional context in comments.

Comments

1

In the below Class A isn't x a local variable too since it is in blocks({})

Your x is not a local variable, it's an instance variable. Block means, instance initialization block or static block or try catch block.

Access modifiers cannot be used for local variables but i am sure i can add public private protected ?

It doesn't make sense to have access modifiers to method local variables, since, method local variable can only be accessed inside the method.

Comments

0

I'm not entirely sure where you are reading. Local variables are values declared inside a method.

There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method.

Taken from here.

Comments

0

A local variable is a variable that has a local scope and is generally not expected to last a long time.

If you declare a variable inside a method, that variable will only be relevant while that method is being executed, and after the method finishes the variable will be discarded.

An instance variable is a variable that is bound to an instance of "something" and is generally considered to last as long as that "something" lasts.

If you declare a variable inside a class, then when you create a new instance of the class the variable will also be created and pinned onto the class. The variable will be there as long as the instance object of the class that you created exists, if you decide to discard the instance of the class you made, then the instance variable gets discarded as well.

Hope this was intuitive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.