Can we access a non-static variable in the static context on java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Can we access a non-static variable in the static context on java?

18th Jun 2021, 4:01 AM
RuBeen Dahal👿
2 Answers
+ 1
why should be easy to understand. accessing non-static elements means accessing members of an object of the class to which the static element belongs. but since the static element is shared among all the objects of that class and exists in the moment in which a class is defined without the need for an object to be instantiated, how can a static method choose and access non-static elements, therefore the members of a object that may not exist, be one or n?
18th Jun 2021, 5:21 AM
Ciro Pellegrino
Ciro Pellegrino - avatar
0
You have to make the objects of them to acess in main
18th Jun 2021, 10:53 AM
Atul [Inactive]