Search code examples
javaiostreamprintwriterautocloseable

Will a PrintWriter without assignment to a variable close after using?


Is

new PrintWriter(ExampleStream).print("Just Something");

same as

PrintWriter pw = new PrintWriter(ExampleStream);
pw.print("Just Something");
pw.close();

So will the PrintWriter close?


Solution

  • No.

    And technically the term anonymous in Java refers to creating a local class without a name. What you did is just creating an instance of a class without assigning it to a variable.