#Static or Constructor

1 messages · Page 1 of 1 (latest)

fickle tigerBOT
#

<@&987246399047479336> please have a look, thanks.

fickle tigerBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

sly sapphire
#

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.

  1. Static - Import it as static and call the method directly
  2. 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

sly sapphire
#

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();
}
lilac panther
#

depends on what doSomething does

sly sapphire
#

It calls the returnString

lilac panther
#

the big downside of a static method is that you cannot "mock" it in testing

sly sapphire
#

Oh how do they get tested they don't?

lilac panther
#

what

#

Oh how do they get tested they don't?

#

i don't know what this means

sly sapphire
#

How do people test the static method then

lilac panther
#

well by calling them

#

its more like

sly sapphire
#

Oh isn't that easier

lilac panther
#

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

sly sapphire
#

Ahhhh yeah that makes sense

#

That is an example but could return an object or anything

lilac panther
#

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"

sly sapphire
#

Why not as doesn't import static do thre same as above just avoids doing the ClassOne

lilac panther
#

exactly

#

something that is hard to internalize i think for some people

#

code is read

sly sapphire
#

Ahhhh Yesss I got you

lilac panther
#

way more than code is written

sly sapphire
#

So overall though nether option is a disadvantgae or adventage except for testing?

lilac panther
#

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)