1

For example

Class Moon{


    int a;

    void mango()
    {
        a=9; // put 9 in instance variable "a"
    }

    void orange()
    {
        int a; // declare local variable with same name as of instance variable

    }

}

How this is possible that we can modify the value of instance variable inside method and also we can declare the local variable with same name as of instance variable?

8
  • 1
    Why shouldn't it be possible? Both variables have different scopes. Commented Oct 18, 2016 at 9:45
  • how to get that which variable to use ..suppose i have declared local variable with the same name as of instance then if I want to access the value of instance variable in same method how will I do it? Commented Oct 18, 2016 at 9:52
  • this.a; or a;. If you're inside the method orange() and call a, you'll get the method variable. If you use this.a, you'll get the class variable. Elsewhere, calling a will get the class variable Commented Oct 18, 2016 at 9:55
  • Abhishek, your question was quite unclear since you didn't say that you want to access a (from the instance) in orange. I've updated your question to make this more clear. Commented Oct 18, 2016 at 9:58
  • 1
    @Tom by editing the question, you have actually changed its meaning. The question was simply asking "how come this is possible". You changed it to "how can I..." --Not that I mind, but this is something to be careful about. People can get upset by things like these. Commented Oct 18, 2016 at 10:08

2 Answers 2

1

When there is an instance variable and a local variable you can use the instance variable with this.a and use the local with just a.

Common use:

public void setA(int a) {
    this.a = a
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can I say this is a Bug in a compiler . because we have access to two variable with same name .. like if 'a' is instance variable then we can write public void setA() { a=20; int a=30 } ..we are using both variable in the same method .
That's not a bug, just sloppy programming from the programmer.
I wouldn't say sloppy. For setter methods, it's literally the best name for the parameter as it's called exactly what it is setting.
1

Modifying instance variables from within methods is:

  1. prescribed by the java language specification, and
  2. pretty much necessary: that's how object-oriented programming works.

(I mean, if you could not modify an instance variable from within a method, then where would you modify it from?)

If you want a particular instance variable to not be modifiable from within methods, then you must mark it with the final keyword, like this: final int a; --you will still be able to initialize it from the constructor.

Declaring a local variable with the same name as an instance variable is perfectly valid java; however, it is not advised, and that's why you can usually configure your IDE to issue a warning when you do that. Actually, if your IDE is not giving you a warning, then this may mean that you are not compiling with enough warnings enabled. Always enable as many warnings as you can.

2 Comments

ah alright . I want to add something . You said final variable can be modifiable inside constructor but it should be final variable can be initialized inside the constructor . You cannot modify the value of final variable once assigned .
Well, yes, that's right, it can be initialized, not modified.

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.