Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using same function in multiple class

Tags:

java

function

I got some function I would like to use in multiple classes, what is the proper way to do that ?

I thought creating a class and set my functions to static... or maybe implement this class who need these functions ?

What do you think ?

Thanks

like image 373
Shinao Avatar asked Aug 01 '26 04:08

Shinao


1 Answers

If this method does not use any instance variable of any object, it can (and probably should) indeed be made static, which makes it available from everywhere (provided it's also public).

Good examples of such stateless, globally available methods are the methods from the java.lang.Math class.

like image 115
JB Nizet Avatar answered Aug 03 '26 17:08

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!