Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a static variable (global variable) be an object in Java?

Ok, off the bat, I know this question either sounds like common sense, or very confusing due to its wording. Unfortunately, due to my lack of knowledge even after much research, I am unsure how to phrase it. To my understanding at this point, static variables do not refer to an instance of a class, and a class.method call is used to access them. variables declared non-static and public are instances of the class and can be called using an object.method call. However, in recent examples, I have seen statements such as:

private static Rectangle rect; 

why not just write

public Rectangle rect;

How is this possible? I thought that static variables were not associated with instances of classes? I am just very confused regarding the meaning and limitations/usages of static versus non-static. I apologize for any confusion. If this question deems unanswerable, a link providing an example of SIMPLE differentiation between static and public usage would be greatly appreciated. I am a complete beginner so please avoid using too much technical jargon. Thanks in advance.

like image 367
imgolden62 Avatar asked Dec 05 '25 10:12

imgolden62


1 Answers

public/protected/private have nothing to do with static.

static means a member (be it a data member or a method) belongs to the class, and not to a specific instance.

public/protected/private just control what other classes can access these members, be it instance members or static members.

like image 77
Mureinik Avatar answered Dec 07 '25 00:12

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!