Does Java have a using statement?

前端 未结 12 2002
梦谈多话
梦谈多话 2020-11-28 16:13

Does Java have a using statement that can be used when opening a session in hibernate?

In C# it is something like:

using (var session = new Session()         


        
12条回答
  •  生来不讨喜
    2020-11-28 17:10

    Since Java 7 it does: http://blogs.oracle.com/darcy/entry/project_coin_updated_arm_spec

    The syntax for the code in the question would be:

    try (Session session = new Session())
    {
      // do stuff
    }
    

    Note that Session needs to implement AutoClosable or one of its (many) sub-interfaces.

提交回复
热议问题