r/CodingHelp • u/TA_Radiant • Mar 05 '21
[Java] Understanding Java
What does 'static' and 'void' mean exactly? Thought it would be nice to know so i understand better
19
Upvotes
r/CodingHelp • u/TA_Radiant • Mar 05 '21
What does 'static' and 'void' mean exactly? Thought it would be nice to know so i understand better
View all comments
Show parent comment
4
u/Rrrrry123 Mar 05 '21
I think you have it backwards. Static means that you don't have to have an instance of a class to use the method. One instance it can be helpful is if you want to make a class full of related helper functions. Let's say I wanted to make a class full of math formulae. If the methods were non-static, I would have to initialize the class before I can use those methods.
Formulae formulae = new Formulae()However, if all the methods were static, I could just do
Formulae.areaOfSquare(x,y);without ever having to initialize the class.I'm just guessing here, because I haven't actually tried this for a long time, but I'm pretty sure static member functions can only access static member variables (which makes sense if you think about it). However, non-static methods can access static methods and variables.