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 trialUnsubscribed User
1,785 PointsI don't really get what public, private and static are for?
Could explain me when to use it and what it's for?
Btw: Why should I use final if I can just set it to private?
2 Answers
Cindy Lea
Courses Plus Student 6,497 PointsIn simple terms: private hides from other classes within the package. Public exposes to classes outside the package. In Java, a static member is a member of a class that isnβt associated with an instance of a class. Instead, the member belongs to the class itself. As a result, you can access the static member without first creating a class instance.
Marcus Mingo
18,424 PointsCindy covered just about everything. Think of 'static' as Java's way of making a member function or variable readily available to any part of the entire program. If you have a static constant variable that you need to use in every package of your entire program, you would just have to import the proper file and call the variable as you would any other member method or variable. With static the changes to a non-static variable can override the original data, so if the intent is to provide something more incremental in purpose, then there is that use as well. Some people refer to static as Java's version of global.
Rob Watts
2,739 PointsRob Watts
2,739 PointsNice explanation. The static keyword is particularly confusing.