How is a static variable (global variable) a Java object?
Okay, right off the bat, I know this question sounds like common sense or very confusing because of its phrasing. Unfortunately, due to my ignorance, even after much research, I'm not sure how to formulate this. As far as I understand, static variables do not belong to an instance of a class, and a call to the .method class is used to access them. variables that are declared non-static and public are instances of the class and can be invoked by calling object.method. 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 static variables are not associated with class instances? I'm just very confused about the meaning and limitations / usages of static versus non-static. I am sorry for any confusion. If this question is considered irrefutable, a link providing an example of a simple differentiation between static and public use would be appreciated. I am a complete beginner, so please avoid using too much technical jargon. Thanks in advance.
source to share
public/protected/private
have nothing to do with static
.
static
means a member (be it a data item or a method) refers to a class , not a specific instance .
public/protected/private
just control what other classes can access these members, be they instance members or members static
.
source to share
Consider FirstClass
and SecondClass
below:
public class FirstClass {
private static Rectangle rect;
// getters and setters
}
and
public class SecondClass {
private Rectangle rect;
// getters and setters
}
You can create as many instances FirstClass
and SecondClass
as you like. However, it is rect
handled differently by the two classes because of the keyword static
:
- In case
FirstClass
rect
is a class variableassuming there will only be one instance referenceRectangle
. Note that depending on the implementation,FirstClass
you may or may not need to instantiateFirstClass
at all, and there is still a link torect
. - In case
SecondClass
,rect
is an instance variable meaning that each instanceSecondClass
will always refer to instance aRectangle
. However, these links do not exist if not createdSecondClass
.
I suggest you read Introduction to Class Members in the Official Java Tutorial. From the first line:
keyword
static
for creating fields and methods that belong to the class, not an instance of the class.
source to share
To answer the difference, consider this example:
class MyClass {
public static Rectangle rect = new Rectangle();
public Rectangle rect1;
public MyClass(Rectangle rect1) {
this.rect1 = rect1
}
}
Now if I like:
MyClass clazz1 = new MyClass(new Rectangle());
MyClass clazz2 = new MyClass(new Rectangle());
How many instances do you see as rect and how many are rect1? You can have rect1 associated with each object, while you only have one rect instance shared by each class.
source to share
First of all, to decompose the difference, suppose it is written in some class Foo,
public class Foo{
private static Rectangle rect;
}
This means that rect is a member of the Foo class, which private cannot be accessed from any other class. And being static, you don't need to create an object and will share all instances of Foo.
public class Foo{
public Rectangle rect;
}
This means that rect can be obtained from any other class by instantiating Foo.
When we stat is not associated with any object, it means that you do not need to create an object to access the static variable and share it with all instances. However, private / public / proctected refers to visibility from other classes.
source to share