Using static methods in an enterprise application in JAVA

I am working on an Enterprise Java application, which is essentially an HTTP server. There I should use some constants and some utility functions for parsing, file I / O, etc. For this purpose, I used static methods and variables, but someone pointed out to me that using statics for this purpose is a bad choice, because of performance and memory.

Until I agree with this argument, I also see no other choice. Even if I convert them to instance methods and access through a singleton instance, the instance will be accessible by some static function.

So, I want to know if this is really a problem. And if so, what is the best way to fix this?

Thanks in advance.

+3
source share
7 answers

You are absolutely right that in the end everything starts from a static method main(). And many functions are suitable for static, for example. simple methods such as on java.lang.Math. Speed ​​and memory usage are not a problem - the static method does not take up more memory or runs slower than any other method. Internally, it is exactly the same as any other method, but it is defined and executed on the class instance instead of the class instance.

, , , (, ), . . , , , , ) I/O - . -. , . .

, - . , " " .

, , Apache Commons IO, Google Guava .. , . , .

+4

, , singleton? , . .

" ":

  • " " - alawys , " ". - .
  • " " - , , . . .

- , . , - . , , , , - TDD - , TDD "" , .

+2

  • private, , final, ,
  • , import static SomeClass.someMethod;,
  • , - , 1000 ( ).
+1

- , ,

, ! , .

( / ) .

+1

?

static:

  • 1: Util , , .
  • .
  • Util ( , CDI).

, (, Apache Commons), do .

, , .


:


1: static , PowerMock. , , , , . . ( .)

+1

, Util , . , , .

MyFileReader.readFile("C:/file.txt");

, :

MyFileReader mfr = new MyFileReader();
mfr.readFile("C:/file.txt");

, , , . , . , , ( ), .

0

, . , . , . , , "this".

- . , , . , . - , , - , .

, Singleton .

0

All Articles