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 Java Objects Creating the MVP Remaining Tries

Razvan Cirstea
Razvan Cirstea
2,493 Points

What does " static " imply ?

Hi,

What does defining a variable as "static" imply ? What would be the benefit ?

Thanks !

rajan lagah
rajan lagah
2,539 Points

by declaring variable static u can assign value of that variable to other variable by referring to its address / that 2 variable will refer to same address.

2 Answers

Mike Papamichail
Mike Papamichail
4,883 Points

I'm not sure if this is what it implies, but from what i understand you can declare a variable(or field) as static so that it is accessible(assuming it is also public) straight out of the class without the need of instantiating an object of that class.And, this static field will be available to every single instance of that class that gets created without the need of declaring it every time you instantiate an object of that class.Hopefully i got something right here! Craig Dennis (i would really like to know the "right" answer to this question sir because i'm not quite sure i got it right)! Thanks.

Craig Dennis
Craig Dennis
Treehouse Teacher

Yep that's right Stelios Papamichail !

Static is actually referring to how the variable is stored in memory, so it can be a little misleading/confusing. But you are right, when used on a field, it allows classes to have a single value, even without creating an instance. You can access that field right off of the class instead of needing to create an instance.

Mike Papamichail
Mike Papamichail
4,883 Points

Craig Dennis Thank you very much for your help sir! Razvan Cirstea , i hope this helps mate ;) !

Razvan Cirstea
Razvan Cirstea
2,493 Points

Thank you very much, Stelios and Craig ! It's all clear now :D