1

I am currently working on a Java project that scrapes data off of a website periodically and then stores it in a MySQL database. I am currently using a static method to do this, but I read online that using static methods can be unsafe. I don't entirely understand why. Should I refactor my code to replace these static methods, or is it safe leave it as is? If I should change it, can you explain why?

kunalbuty
  • 95
  • 1
  • 8
  • Could be somewhat related: [Disadvantages of static Connection object in Java standalone application](https://stackoverflow.com/q/48194821/12323248) – akuzminykh Jul 16 '20 at 05:59
  • 1
    C# but definitely related: [Why would I use static methods for database access](https://stackoverflow.com/questions/21413726/why-would-i-use-static-methods-for-database-access) – akuzminykh Jul 16 '20 at 06:04
  • When you read online that using static can be unsafe, didn't they offer you a good reason why? – tifi90 Jul 16 '20 at 06:06

2 Answers2

1

It depends on the case. Static methods are safe if they do not have any state meaning that method does not alter some internal or global - also static - variable.

If method uses and alters static variables it might lead into bugs that are very hard to resolve when there are many threads or so in your aplication.

Also testing with frameworks like Mockito will be hard since mocking static methods needs special handling (like using PowerMock-library).

These facts are amongst the most important reasons why advanced coders might prefer not to use static stuff. What is the point of having "nice and easy" static method if it can generate problems later and causes complications and need for extra libraries in testing (which you eventually have to do)?

Usually a singleton is preferred if there is a need for some internal state. Also for example in Spring framework people tend to avoid static stuff and prefer beans that are singletons by default.

pirho
  • 11,565
  • 12
  • 43
  • 70
0

If this is a single-threaded application maintained by just one developer, it should be fine to leave it as-is.

Are you planning to write Unit Tests? That could become a challenge, for example Mockito will not work with static methods.

Alex R
  • 11,364
  • 15
  • 100
  • 180
  • I am just doing this project as a way of improving my coding skills so I don't think I'm going to go as far as writing unit tests. In general though is it better practice to avoid static methods, or do I just have to learn when to use them? – kunalbuty Jul 16 '20 at 06:17
  • 2
    Static methods are considered a “code smell” in professional code because they get in the way of other common requirements such as multi-threading and unit testing. For small educational or personal projects lacking even simple unit tests it really doesn’t matter, use whatever language features you want to learn. – Alex R Jul 16 '20 at 06:23
  • *"Static methods are considered a “code smell” in professional code [...]"* - I really question if that statement can be done this general ... – akuzminykh Jul 16 '20 at 06:34
  • Sure there are exceptions, not applicable to the OP’s question. – Alex R Jul 16 '20 at 06:39