#Static or Constructor
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Hi All, I am curious so if I make a method I can call it in multiple different ways but I wanted to know between these two which one is better.
- Static - Import it as static and call the method directly
- Use a constructor Eg Random random = new Random(); then call the method
^ Are they both the same or is one worse or better or different use cases or maybe security
Import what as static?
A method
I will write some examples of what I mean give me 2 secs
Example One
ClassOne.java
public String returnString() {
return "Hello World"
}
ClassTwo.Java
public void doSomething() {
ClassOne classOne = new ClassOne();
classOne.returnString();
}
Example Two
ClassOne.java
public static String returnString() {
return "Hello Wolrd"
}
ClassTwo.java
import static org.java.ClassOne.returnString;
public void doSomething() {
returnString();
}
depends on what doSomething does
It calls the returnString
the big downside of a static method is that you cannot "mock" it in testing
Oh how do they get tested they don't?
How do people test the static method then
Oh isn't that easier
if you want to test something that uses a static method, but you want to do something different than what the static method does
say it made an API call or whatever
and you can't call the API in test
then you have a problem and you probably want the "seam" that an object reference gives you
but if its literally just returning a string it doesn't matter
Ahhhh yeah that makes sense
That is an example but could return an object or anything
and it double doesn't matter if you are calling new Thing() right before calling the method
since you don't have a seam regardless then
so for your specific example, a static method is better, but i wouldn't do the import static
import org.java.ClassOne;
public void doSomething() {
ClassOne.returnString();
}
but the real "answer" is "it depends"
Why not as doesn't import static do thre same as above just avoids doing the ClassOne
exactly
something that is hard to internalize i think for some people
code is read
Ahhhh Yesss I got you
way more than code is written
So overall though nether option is a disadvantgae or adventage except for testing?
in your exact example neither has an advantage in testing because your object construction is colocated with the usage of the method
and the static method is better given that because the thing being done doesn't rely on instance state
(your example is too dumb to give nuanced advice about)