T
T
tsypanov2017-09-25 18:07:14
Java
tsypanov, 2017-09-25 18:07:14

Which class to use in an expression like Class.getClassLoader().getResourceAsStream()?

There is a code:

class ThisClass {
  void foo() {
    InputStream resourceAsStream = ThisClass.class.getClassLoader().getResourceAsStream(path);
    //...
  }
}

Is it desirable to call getClassLoader() on the class containing the specified code (in this case ThisClass ), or is any other class appropriate?
What side effects can accompany the execution of such code:
class ThisClass {
  void foo() {
    InputStream resourceAsStream = ForeignClass.class.getClassLoader().getResourceAsStream(path);
    //...
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-09-25
@tsypanov

No difference. Almost always, the getClassLoader() method will return the same loader for any class in your application. Also, it is easier and shorter to use the getClass().getResourceAsStream(path).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question