0

Can we call a instance variable in a static method other than static main ,how?

5th Dec 2019, 1:08 PM
Ajoh Pv
Ajoh Pv - avatar
6 Réponses
0
Edit: You can't call a instance variable even inside the main method without creating an instance of the class. But you can always create a variable with the same name as instance variable inside a static method, this process is called shadowing. Non-static variables are not accessible inside the static context.
5th Dec 2019, 1:21 PM
Avinesh
Avinesh - avatar
0
but i can call a instance variable in a static main by first creating a object //A ref=new A(); and then ref.variable name // this can be done right?
5th Dec 2019, 1:26 PM
Ajoh Pv
Ajoh Pv - avatar
0
Yes because you are accessing with the help of an object.
5th Dec 2019, 1:29 PM
Avinesh
Avinesh - avatar
0
What ever, you are able to in main method, you can do it in any other static methods...
5th Dec 2019, 1:38 PM
Jayakrishna 🇮🇳
0
jaya krishna are you saying that I need to create a object for that.
5th Dec 2019, 1:41 PM
Ajoh Pv
Ajoh Pv - avatar
0
Yes. In same way as it possible in main method..
5th Dec 2019, 1:45 PM
Jayakrishna 🇮🇳