+2 Developer John · January 16, 2015
Okay, I've been reading on what static truly means in Java from the Oracle, and yet it explains it stating that you use it when you want variables to be common to all objects. What does that mean?:ermm:

Post a Reply


Oldest  Newest  Rating
0 Developer John · January 22, 2015
Thanks! I understand!8-)
+1 Kuroodo Ditory · January 22, 2015
Yeah pretty much you can think of it that way. But it's more of a "If you change it, you change it for all of them".

But the way I see it is that when something is static, there is only 1 reference and all objects share that reference, instead of a new separate reference being made for each object. So for a static banana and 5 objects, there would be only 1 banana. But for a none-static banana and 5 objects, there would be 5 bananas.

But yeah that is how static works. Then not making banana static would print out 7,8,9
0 Developer John · January 21, 2015
Okay, so I've tested it, by directly creating three objects for my class. Okay, so on my findings, please correct me if I'm wrong, but I noticed that the default value was simply the same as printed, due to the fact that there were no references to that public static variable(no initialization of that object's variable). The other part I have noticed is that the Java Compiler automatically gets the last reference to that variable(the last object variable assignment statement) and overrides all other values of that variable that were previously initialized. 

For instance:

testObject.banana = 7;
testObject2.banana = 8;
testObject3.banana = 9;

/*All of these would print out 9 since the testObject3.banana statement is the last statement that initializes the variable banana.*/

0 Kuroodo Ditory · January 19, 2015
Here make a new project and make 3 classes called Apple, Hello, and Init.

Then just copy and paste the code from these files into each class.

Run the program and see what happens (the Init class runs the code). Then change values around and try not making banana static.

Let me give you an overal explenation of what happens:

banana is set to static and has a default value of 5.

First we set banana = 2 in myApple. Then we print out banana from myApple and mySecondApple. Both print out 2. If banana wasn't static then myApple would print out 2 and mySecondApple would print out 5.

Then we made a new object for the Hello class. The Hello class also makes an object for the apple class and sets the value of banana to 100 and then prints it out.

Back in the Init class, we print out myApple and mySecondApple again right after creating our Hello object. You will see that all 3 print out 100. If banana wasn't static, it would print out 100 for hello, 2 for myApple, and 5 for mySecondApple.

Now, how can static be useful? 

Let's say you had a class called ViewPort. The viewport class basically handles the width and height of the window of your program. You want the width and height to be static. So sometime throughout your code, your viewport get's changed. If the width and height weren't static, then only a small part of your program will know that the viewport width and height changed. If a class that manages graphics doesn't have the correct viewport, then all your graphics wouldn't look right. But if the width and height were static, then any change would happen for all classes in your program, and your graphics class would display all graphics according the new current viewport.

Another example, but this time from my own code:

I have a class called GameInit that initializes everything needed to start my game. I also have a class called ContentManager that handles and manages all my resources (textures, music, maps, etc).  The content manager loads resources into the game, get's them when needed, and even deletes them (none of the variables in there are static though). In my GameInit class, I create a static ContentManager object called res and load up all my resources. So the only thing static here is the res object. Since it it static, all classes can use res, and the data inside of it would be the same.

So now let's say I made a new character and it needs a texture, I simply (from that character class) just do GameInit.res.gettexture(blah); Now if res wasn't static, I would have to create my own object in that current character class. But upon creating the new object, nothing would be loaded/it wouldn't have any data because it is a completely new object.

If you're still confused, just mess around with static objects,variables, methods, etc. Experimentation is the key to understanding something.
0 Developer John · January 19, 2015
What I don't understand is why myApple.banana was printed out with a value of 10. I still don't understand why it was incremented, due to the fact that I find static and final very similar.
+1 Developer John · January 16, 2015
Yes, I know what objects are, but like what @Kuroodo Ditory said, people need to start talking more clearly. 
+4 Kuroodo Ditory · January 16, 2015
People really need to start speaking human when it comes to beginners/people asking questions xD

Anyway, static is a way of saying that if you change something in a class, all classes will see the change.

Let's say I had a class called Apple.

Here is the code for Apple:

public class Apple{

public int banana = 5;


Notice the variable banana.

So now we have this other class and we create 2 Apple objects.

public class someOtherClass{

public someOtherClass(){
Apple myApple = new Apple();
Apple mySecondApple = new Apple();



No we will be changing the value of banana in for each object and printing it out.

myApple.banana = 5;
mySecondApple.banana = 10;


This would be the output:

But now let's make banana static

public class Apple{

public static int banana = 5;


Now let's run the program again.
The output would be:


Notice how this time both were 10? That is what static does and what it means.

Let me add more info:

myApple and mySecondApple are both objects. Each object has their own values and such. Want proof? (like bucky says hehe) When banana wasn't static, the output was 5 10; why? Because as said, each object has their own value's for variables.

Static basically makes it the opposite.

(i'm trying my best to explain this at 4:30 am lol).

A better example/way of explaining it:
When the banana isn't static, we clone it and give it to both objects. One object might eat their own banana, while the other can sit on theirs or whatever lol.
But when it is static, the banana isn't cloned. It's the original banana. Meaning that they both share it.

Try doing this as well to see it clearer.

myApple.banana = 5;


mySecondApple.banana = 10;



Static methods pretty much work the same way.
0 Mike Conroy · January 16, 2015
Have you learnt what objects are?
If not my advise would be to ignore the word static for now until you understand what an object is.
For the majority of questions I would not suggest to just ignore things as it is important to understand why something is the way it is. However, in this case without an understanding of objects it is nearly impossible to explain clearly.
0 Developer John · January 16, 2015
What do you mean by that?
0 Mr. Computer · January 16, 2015
 keyword to create fields and methods that belong to the class, rather than to an instance of the class.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator