• Post Reply Bookmark Topic Watch Topic
  • New Topic

Am I understanding the static keyword correctly?  RSS feed

 
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static keyword as I understand it, means "associated with the class as a WHOLE and not any one specific object, and we don't need an object to use static things". We have two static things to consider, static variables and static methods. Static variables like static methods can be used without the instantiation of an object.
An example that come to mind would be:




I think of a static variable as being ONE THING and therefore anytime it's changed somewhere in the program that ONE THING gets changed, as opposed to instance variables which are not one thing but are sort of dolled out on an individualized basis. Like once an objects gets created and each object gets their own individualized set of instance variables, therefore the change of one instance variable would not affect the other instance variables of other object, they are not connected in that way. Each object gets their very OWN copy that's soley theirs only.

This is different with static variables, when an object accesses a static variable and changes it that change is seen throughout the ENTIRE class because like before, static variable are ONE THING, and when that ONE THING gets changed that change is seen everywhere else in the class because it's a SINGLE THING that all the objects and class holds on to at once. It's like everyone is stretching their arms out and holding onto a single piece of clay and when one person object or CLASSname comes along and changes it everyone else sees that change because it's shared amongst all of them. Whereas instance variables, that's on an individual basis and each person gets their own copy, maybe that be a shovel, maybe their name, maybe a hat, a lot of things can be represented with instance variable. I guess an example of static variable that comes to mind in a real-life example and which everyone shares is time, that is if we're in the same state. So when the time changes everyone sees that state change. But people's names and ages, those are individualized to the specific object, they don't change if someone else changes their own version.

My example of static used in a time example:




Grateful for all the help I've received so far. Would be greatly appreciated to have this cleared up.

Thank you in advance!


 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . .
num1 = 6; // is there any difference between assigning it here vs up top, or inside the method itself or inside the main method?
Yes. You can say System.out.println(Mathy.num1); and get 6 without having to call any of the methods first. But it is still not a good idea to have non‑private variables.
. . .
public static int addEmUp(int n1, int n2) { // must have static keyword in method declaration . . .
I would write...etc. Those methods are pure functions: they always return the same result from the same inputs, they have no side‑effects, and they do not need or alter any information from an instance of their class. They shou‍ld therefore be considered candidates for being static.
. . .
Mathy.addEmUp(num1, num2); // is it true we don't even need non-static variables we could just write in numbers here?
addEmUp();
Not quite sure I understand that bit, but what I did understand looks correct. You can write int i = Mathy.add(123, 456); but if you don't use the returned value it will disappear into cyber‑limbo never to be seen again when the current method completes. Mathy.add(); won't compile because you aren't passing the right arguments.
. . . I think of a static variable as being ONE THING and therefore anytime it's changed somewhere in the program that ONE THING gets changed, as opposed to instance variables which are not one thing but are sort of dolled out on an individualized basis. Like once an objects gets created and each object gets their own individualized set of instance variables, therefore the change of one instance variable would not affect the other instance variables of other object, they are not connected in that way. Each object gets their very OWN copy that's soley theirs only.

This is different with static variables, when an object accesses a static variable and changes it that change is seen throughout the ENTIRE class because like before, static variable are ONE THING, and when that ONE THING gets changed that change is seen everywhere else . . .
Not quite the formal scientific way you would express it if you were presenting a paper (‍) but that all sounds correct.
Grateful for all the help I've received so far. Would be greatly appreciated to have this cleared up.

Thank you in advance!
Our pleasure

There is another pitfall with static methods. They are not overridden. Once you get used to overriding instance methods and having them behave polymorphically in subclasses, there is a temptation to try polymorphism with static methods, and it comes as an unpleasant shock to find the code doesn't work as you had expected. That is why static members shou‍ld always be invoked as ClassName.memberName, not objectReferenceName.memberName.
If you want a bit of crappy code to prove that instances are not necessary for static members, find out about this method and this one.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say that I can write:
Campbell Ritchie wrote:
System.out.println(Mathy.num1); and get 6 without having to call any of the methods first.


This is only made possible because num1 is static, if it was an instance variable it wouldn't work because you need an object to call object variables.

Is this correct?

For variables:
Class.classVariables; // works
Class.objectVariables; // doesn't work, need an object for that
Object.classVariales; // works, but shouldn't be done
Object.objectVariables; // works

For methods:
Class.classMethod; // works
Class.objectMethod; // doesn't work, need an object for that
Object.classMethod; // doesn't work, need a class for that
Object.objectMethod // works

What I gather from that is "like variables and methods with like classes and objects", is that the general rule of thumb to follow?

Any variable and method that's assigned with the static keyword should only be used in conjunction with the className and dot operator
Any variable and method that is not assigned with the static keyword is therefore associate with objects an should only be used with the objectName and dot operator

Thank you once again!

 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Since this method is not declared staqtic, you will need an instance of Mathy to use it. But since it does not depend on any instance variables, it should be static, just like addEmUp.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:
Is this correct?

For methods:
Object.classMethod; // doesn't work, need a class for that


Well, since an object is an instance of a class, if you have an instance, don't you know the class type?

Did you try this?

Henry
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:When you say that I can write:
Campbell Ritchie wrote:
System.out.println(Mathy.num1); and get 6 without having to call any of the methods first.
I meant that if you initialised that number in one of the methods, that code would display 0 until the method is run. It is probably better to initalise static fields as soon as they are declared, rather than using the constructor.
This is only made possible because num1 is static, if it was an instance variable it wouldn't work because you need an object to call object variables.

Is this correct?

For variables:
Class.classVariables; // works
Class.objectVariables; // doesn't work, need an object for that
Object.classVariales; // works, but shouldn't be done
Object.objectVariables; // works

For methods:
Class.classMethod; // works
Class.objectMethod; // doesn't work, need an object for that
Object.classMethod; // doesn't work, need a class for that
Object.objectMethod // works
Your 7th example is wrong, it should read
Object.classMethod; // works, but shouldn't be done (Same as earlier)
The other 7 are correct. You sh‍ould normally not be able to access fields outside their classes because good design would make all fields private.
It is probably better to say static method than class method and instance method rather than object method. And leave that abomination “non‑static” to the compiler error message
What I gather from that is "like variables and methods with like classes and objects", is that the general rule of thumb to follow?
Do you mean that you shou‍ld invoke all methods and reference all fields with similar syntax? ff so, yes.
Any variable and method that's assigned with the static keyword should only be used in conjunction with the className and dot operator
Any variable and method that is not assigned with the static keyword is therefore associate with objects an should only be used with the objectName and dot operator
It is impossible to use an instance member without knowing which object it belongs to. I think you sh‍ould say declared rather than assigned. Yes, it looks like jargon, but jargon is there so we all know exactly what each other means.


Thank you once again!

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!