Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterless methods/static

Tags:

java

As my name suggests, I am a .NET developer but I have a growing interest in Java, and I am interested in learning more about other languages as this helps me to learn more about programming generally.

Anyway, my question is this: Methods which don't take parameters/work with state (which is just parameters in the method, correct me if I am wrong) are recommended to be made static. What is the relationship/link between static and parameterless methods? Not working with state means if you pass a Person object into the method, and you don't edit that object's state (Eg its properties) - this is my understanding.

I don't mind any Java specific answers.

Thanks

like image 621
GurdeepS Avatar asked Sep 02 '26 22:09

GurdeepS


1 Answers

"What is the relationship/link between static and parameterless methods? "

None.

"Methods which don't take parameters/work with state... are recommended to be made static"

Really? By whom? Can you provide a link or quote?

Static means that the method belongs to the class -- as a whole -- not any specific object of that class. Therefore, static methods can only deal with static variables, not instance variables.

Parameterless doesn't mean anything. It may be that the method deals only with instance variables or only with static variables. Or it returns a constant. Or it has some calculation which is private to that method. It could, for example, create a socket, do a read using HTTP, and destroy the socket. No parameters; no instance variables.

like image 127
S.Lott Avatar answered Sep 05 '26 12:09

S.Lott



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!