0

Until now I wrote my programs by using only static methods. I haven't abused the principles of OOP at all. My question is, when should I start working with objects instead of using static methods? Because it seems to me that using static methods makes my life easier, but I might be wrong.

7
  • If you posted some code examples of your code, maybe we could see how your code could benefit from using objects. Commented Mar 25, 2014 at 21:43
  • 1
    Keep on going with static methods if they make your life easier. I always start my design the same way and wait for a real concern to force objects on me. It usually does happen, though. Commented Mar 25, 2014 at 21:43
  • Well it seems to me that everything can be written using static methods. But I might be wrong, as I said. @MarkoTopolnik In what case do you get forced to use objects? Commented Mar 25, 2014 at 21:44
  • 1
    a program made up of static methods is going to be very tightly coupled and hard to test parts in isolation Commented Mar 25, 2014 at 21:47
  • 1
    A third, very important reason is leveraging dynamic dispatch, which can make your code much cleaner. Commented Mar 25, 2014 at 21:50

3 Answers 3

3

Static Methods make your live easier, because you dont have to worry about Accessability in different scopes (not talking about private/public Methods - static methods are ALWAYS there, no matter in which context you are). Basically every OOP Method can be converted to a static method, using the object as one of the parameters. Also each Static method, having an object as a parameter could be converted to a method on the object instance.

People have been developing Apps, before any sort of "OOP" was known, so its not a "musst have".

General speaking: Does your method require "Object Properties"? Use an Object/instance method. Does your method NOT require Object Properties? Use a static method.

OOP makes your live easier, when you have coupled data (like a person has a certain fore- and surename and an email adress -> create an object with those 3 attributes). Instead of passing 3 Parameters to a method, you could implement a method without parameters on the object, and have access to all 3 values.

You would not run into trouble, swapping forenames or email adresses somewhere. Your Object clearly keeps track of data relation.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't write everything using static methods. Just as one example, if you create a JFrame and want to use a control that custom paints itself, you must create a subclass of at least JComponent and override the paint methods.

2 Comments

So in essence, the complexity of the problem or what technologies you want to use decide it?
That's because JFrame is already an object. You should instead prove that it is impossible to design a GUI framework without objects.
0

As long as you don't need any state in your class, static methods are fine. It is also harder to test.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.