Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 114625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:53:26+00:00 2026-05-11T02:53:26+00:00

It’s been at least 5 years since I worked with Java, and back then,

  • 0

It’s been at least 5 years since I worked with Java, and back then, any time you wanted to allocate an object that needed cleaning up (e.g. sockets, DB handles), you had to remember to add a finally block and call the cleanup method in there.

By contrast, in C++ (or other languages where object lifetimes are deterministic, e.g. Perl), the class implementor would define a destructor function that performs the cleanup whenever an object of that class goes out of scope. The advantage of this approach is that the user of the object can’t forget to clean it up — the destructor gets called automatically, even if an exception is thrown. This approach goes by the fairly awful name of RAII — ‘Resource Acquisition Is Initialisation’.

It’s been my experience that doing things ‘the RAII way’ has saved me a lot of mental overhead in terms of not having to worry about whether and when resource deallocations occur. We are considering using Java for a medium-sized project, and I’m wondering if some sort of deterministic destruction is among the many new features added to the language since I last looked at it. (I’m hopeful as my complaint that ‘Java has no RAII’ was rebuked on this thread, but so far I haven’t been able to find any details by googling.)

So if someone could point me to some introductory material on how to go about this in Java that would be great!

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:53:27+00:00Added an answer on May 11, 2026 at 2:53 am

    EDIT: The answer below was written in early 2009, when Java 7 was very much still in flux.

    While Java still doesn’t provide guarantees around finalization timing, it did gain a feature like C#’s using statement: the try-with-resources statement.


    No, Java hasn’t changed at all in that respect. You still need to use try/finally.

    There’s been discussion of adding the equivalent of C#’s ‘using’ statement (which is syntactic sugar over try/finally) to Java, but I don’t think that’s going to be part of Java 7 any more. (Most of the language improvements seem to have been dropped.)

    It’s worth understanding that there are reasons why deterministic destruction hasn’t been implemented in Java and .NET in the form of a reference-counted garbage collector, by the way – that a) impacts performance and b) fails with circular references. Brian Harry wrote a detailed email about this – it’s about .NET and it’s rather old, but it’s well worth a close read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Related Questions

Loading...

Sidebar

Ask A Question

Stats

  • Questions 54k
  • Answers 54k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I would put the repeated elements in arrays and then… May 11, 2026 at 7:26 am
  • added an answer Checkout Merapi (http://www.merapiproject.net/) Java-to-AIR bridge. May 11, 2026 at 7:26 am
  • added an answer You could try handling one of the SelectedIndexChange events. With… May 11, 2026 at 7:26 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.