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

C# C# Objects Methods Static vs Instance Methods

Why not make all methods static?

To use a static method you don't have to create a new object first. Great! Why not make all methods static?

3 Answers

I believe itโ€™s because the objects would not to be able to have different properties from each other. If a static color is orange it will be orange for object a and object b. If one changes it, it will change the otherโ€™s color as well as the property belongs to the class.

Alec Meyer
Alec Meyer
6,303 Points

static methods are usually created as "utility methods".

A good example of using static methods is the Math class. No need to create math objects, but it is super useful to bundle all math related functionality with one place to find it.

static methods lose a lot of power though as they cannot access member variables in an instance of a class. If we made Math objects for some reason, none of these methods would be able to access the newly created object's properties.

Hope that helps.

Ricardo Peรฑa  Marin
Ricardo Peรฑa Marin
15,177 Points

Static methods by definition cannot access instance variables, so you could make all methods and variables static, but practically you would't have objects.