 |
 |
How to use 'static' members properly in Java?
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
I'm having some difficulties with understanding the whole 'static' concept in Java. The tutorials aren't much help. I know that static variables are the same for all instances of the class, and I know that static variables can't be manipulated by non-static methods and vice-versa. I also understand that in order to manipulate non-static members I must for make an instance of the class and manipulate them through it.
But what I don't understand is how to know if this variable, or this method should be static or not, and what benefits that would bring? And what do I do if I want to manipulate a static variable in a non-static method?
Thanks for any help.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Originally posted by itistoday:
I'm having some difficulties with understanding the whole 'static' concept in Java. The tutorials aren't much help. I know that static variables are the same for all instances of the class, and I know that static variables can't be manipulated by non-static methods and vice-versa. I also understand that in order to manipulate non-static members I must for make an instance of the class and manipulate them through it.
But what I don't understand is how to know if this variable, or this method should be static or not, and what benefits that would bring? And what do I do if I want to manipulate a static variable in a non-static method?
Thanks for any help.
Static versus not-static messes up people all the time. Static variables, also called Class variables, exist only once in memory. So if I had a Clock class with two static variables hour and minutes, then there would be one spot in memory for the hours and one spot in the memory for minutes. If I made multiple Clock's via the new command, I would still have only one memory location for each variable... so each Clock would have to share the same time!
That is a very good case to use an instance variable instead. If you wanted to have a variable that is the same for all Clock's (for example, a reference time in milliseconds where all Clock's should be referenced against), well since all the Clock's will have the same value for that variable, it's safe to make it static.
Static variables are usually used for final fields of the class, like Math.PI, which is a value which we shouldn't be able to change (so its final) and we don't need multiple versions of it running around in memory (so its static). (You can't actually instantiate the Math class anyway because it has only one constructor, but its declared private).
In fact, static varaibles can technically be accessed anywhere (as long as you specify the name of the class the variable is defined in). So in an instance method of a class, you can safely modify the static variables. That's not the same for the reverse. If you were inside a static method, you are not inside an actual Clock, just the ClockMaker. So if you made five different Clock's in memory with different hours and different minutes, and you try to modify an instance variable like hours, the question becomes, "Which Clock am I modifying?" The reason that question arises is that each Clock has its own memory location for their hours and minutes variable.
Does that make any sense?
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
Originally posted by Ghoser777:
Static versus not-static messes up people all the time. Static variables, also called Class variables, exist only once in memory. So if I had a Clock class with two static variables hour and minutes, then there would be one spot in memory for the hours and one spot in the memory for minutes. If I made multiple Clock's via the new command, I would still have only one memory location for each variable... so each Clock would have to share the same time!
That is a very good case to use an instance variable instead. If you wanted to have a variable that is the same for all Clock's (for example, a reference time in milliseconds where all Clock's should be referenced against), well since all the Clock's will have the same value for that variable, it's safe to make it static.
Static variables are usually used for final fields of the class, like Math.PI, which is a value which we shouldn't be able to change (so its final) and we don't need multiple versions of it running around in memory (so its static). (You can't actually instantiate the Math class anyway because it has only one constructor, but its declared private).
In fact, static varaibles can technically be accessed anywhere (as long as you specify the name of the class the variable is defined in). So in an instance method of a class, you can safely modify the static variables. That's not the same for the reverse. If you were inside a static method, you are not inside an actual Clock, just the ClockMaker. So if you made five different Clock's in memory with different hours and different minutes, and you try to modify an instance variable like hours, the question becomes, "Which Clock am I modifying?" The reason that question arises is that each Clock has its own memory location for their hours and minutes variable.
Does that make any sense?
That makes perfect sense Ghoser777, thank you. So, if I'm inside a non-static method, and I want to use a static variable in the class, I just do: NameOfClass.variable = Blah?
I'll try it out right now to see if it works actually... Thanks again.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|