Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

What is the term "static" used for?

I am finding it difficult to understand the the use if static in java, could anyone please help me?

2 Answers

Hi Jiten Mistry,

with the keyword 'static' you are declaring a variable as a class variable instead of an instance variable. Now, what is the difference between a class variable and an instance/member variable?

I'll try to explain as simple as I can (it isn't complicated though). Static variables are global and are not specific to an object. If you declare a variable - let's say the variable name is 'cost' - without the keyword 'static', every object you create will have it's own variable 'cost' with it's own value. So you could have 3 car objects which have their own value in their costs variable (or you could say they have their own box).

// cost is not static

bmw.cost = 100; 
mercedes.cost = 500; 
chervolet.cost = 300;  

Every car holds it's own value (in their own box) and if you would print those values it would be exactly what you'd expect.

// cost is static

bmw.cost = 100; 
mercedes.cost = 500; 
chervolet.cost = 300; 

System.out.println(bmw.cost); // output 100
System.out.println(mercedes.cost); // output 500
System.out.println(chervolet.cost); // output 300

Static variables are different. All objects share static variables (they share on and the same box) and thus don't have their own. Let's say you declared the same variable 'cost' as static and would use the same code as I wrote.

Hint which is not necessary to understand static variables: Java would implicitly replace the car objects (bmw, mercedes, chervolet) with the associated class name for example 'car'.

// cost is static

bmw.cost = 100;   // would get converted to car.cost
mercedes.cost = 500;  // would get converted to car.cost
chervolet.cost = 300;   // would get converted to car.cost

They all would alter the same variable (the same box). So first, the box cost hold 100, then it gets altered to 500 and then to 300. If you would print the values out like this:

// cost is static

bmw.cost = 100; 
mercedes.cost = 500; 
chervolet.cost = 300; 

System.out.println(bmw.cost); // output 300
System.out.println(mercedes.cost); // output 300
System.out.println(chervolet.cost); // output 300

You probably have to read through 2 - 3 times. Could I clear things up a little?

Highly recommend you to watch these videos by TheNewBoston - http://youtu.be/Mhxp5dZOy78 and http://youtu.be/14c1oJjgC8g