• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous inner class compiles without ; at the end in try-with-resources.Please explain the reason

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package ocp;
import java.util.function.*;
public class AutoCloseInner{
public static void main(String...strs){
try(AutoCloseable wt = new AutoCloseable(){
public void close() throws RuntimeException{}
})//anonymous inner class not expecting ; when used in the try-with-resources
{
System.out.println(wt.toString());
}
catch(Exception e){
//if(wt!=null) wt.close();
}
finally{
System.out.println("Gone !!!");
method1();
}

}

public static void method1(){
Predicate p = new Predicate(){
public boolean test(Object o){
System.out.println("I am inside");
return 5>4;
}
};
p.test(new AutoCloseInner());
}

}
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

In future please enclose your code in code tags; it will be much nicer to read.

If you look in the Java® Language Specification, you will find that a try‑with‑resources never has a semicolon before its first { but a semicolon is permitted, not required, before the )
 
Rajeswari Gurusamy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.But My question is about different behavior of anonymous inner class(not expecting };) when providing the implementation  inside try-with-resources.
Anonymous inner class is expecting }; in all other places.please refer method2 in my code. So I guess there are few places like this try-with-resource where Anonymous inner class wont expect };
reply
    Bookmark Topic Watch Topic
  • New Topic