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 trialEric Smailys
1,667 PointsI don't really understand the purpose of Static fields
Having just finished the C# Objects course, there's a lot I'm not 100% sure I've internalized, but the thing that sticks out the most is Static fields and methods; I just don't understand what they're for.
I got from the videos that declaring a field or method as static means that when the code is executed, only one instance of that field or method ever exists regardless of how many times the class is instantiated into an object, and all class objects share that single static instance of the field or method. What I don't understand is, why? What scenario does this work in when having a non-static version of the field or method wouldn't?
3 Answers
Steven Parker
231,236 PointsSometimes you might use a static field to save resources where you could use an instance variable. But generally you would use instance variables to represent concepts that are unique per instance (such as "Name" in class "Student"). And you would use static fields to represent constants or shared values (such as "TotalClassSize").
Similarly, methods that do the same thing for each instance might be shared by making them static (like "PickRandomDesk"). But methods that might need to be unique for the instance (or overridden) would be instance methods.
Galen Goforth
Courses Plus Student 1,412 PointsAdding on to what Steven said, I think that a static method is a great way to accomplish something that needs to be done in multiple locations like Math.Sqrt. We shouldn't have to create an instance of the Math class each time we want to use the logic to get the square root of a number, but we also don't want to have to write that same logic all over our application. Instead we use a static method that performs the calculation, but doesn't require a new object to do so.
Lucas Taves
3,516 PointsNot requiring an instance is a huge bonus for helper methods, but that's kinda of a side effect. The main goal is to have attributes/properties and methods that belong to a class rather than the instance. As to why this could be useful, just think of any scenario you might want information/control over the class or all instantiated objects of that class.
For example, counting or keeping tracking of every object created, say for memory management, so you could have a pool of reusable objects instead of destroying and creating new ones every time. Or for the complete opposite, you want to make sure there's only ever one instance of that class at a single time, and an easy way to make that possible is to make the constructor private, and encapsulate the type creation into a Factory Method (which is static) and makes sure that a new instance is only ever created once. That's actually a very common design pattern called singleton;