You never need to destroy an object | CodeGuru

You never need to destroy an object

Bruce Eckel’s Thinking in Java Contents | Prev | Next destroy an object In most programming languages, the concept of the lifetime of a variable occupies a significant portion of the programming effort. How long does the variable last? If you are supposed to destroy it, when should you? Confusion over variable lifetimes can lead […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 2001
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

destroy
an object

In


most programming languages, the concept of the lifetime of a variable occupies


a significant portion of the programming effort. How long does the variable


last? If you are supposed to destroy it, when should you? Confusion over


variable lifetimes can lead to a lot of bugs, and this section shows how Java


greatly simplifies the issue by doing all the cleanup work for you.


Scoping

Most


procedural languages have the concept of


scope

.


This determines both the visibility and lifetime of the names defined within


that scope. In C, C++ and Java, scope is determined by the placement of curly


braces


{}

.


So for example:

{
  int x = 12;
  /* only x available */
  {
    int q = 96;
    /* both x & q available */
  }
  /* only x available */
  /* q “out of scope” */
}

A


variable defined within a scope is available only to the end of that scope.

Indentation


makes Java code easier to read. Since Java is a free form language, the extra


spaces, tabs and carriage returns do not affect the resulting program.

Note


that you


cannot

do the following, even though it is legal in C and C++:

{
  int x = 12;
  {
    int x = 96; /* illegal */
  }
}

The


compiler will announce that the variable


x

has


already been defined. Thus the C and C++ ability to “hide” a


variable in a larger scope is not allowed because the Java designers thought


that it led to confusing programs.


Scope
of objects

Java


objects do not have the same lifetimes as primitives. When you create a Java


object using


new

,


it hangs around past the end of the scope. Thus if you use:

{


String s = new String("a string");

}
/* end of scope */

the


handle


s

vanishes at the end of the scope. However, the


String

object that


s

was pointing to is still occupying memory. In this bit of code, there is no way


to access the object because the only handle to it is out of scope. In later


chapters you’ll see how the handle to the object can be passed around and


duplicated during the course of a program.

It


turns out that because objects created with


new

stay around for as long as you want them, a whole slew of programming problems


simply vanish in C++ and Java. The hardest problems seem to occur in C++


because you don’t get any help from the language in making sure that the


objects are available when they’re needed. And more importantly, in C++


you must make sure that you destroy the objects when you’re done with them.

That


brings up an interesting question. If Java leaves the objects lying around,


what keeps them from filling up memory and halting your program? This is


exactly the kind of problem that would occur in C++. This is where a bit of


magic happens. Java has a


garbage
collector

,


which looks at all the objects that were created with


new

and figures out which ones are not being referenced anymore. Then it releases


the memory for those objects, so the memory can be used for new objects. This


means that you never need to worry about reclaiming memory yourself. You simply


create objects, and when you no longer need them they will go away by


themselves. This eliminates a certain class of programming problem: the


so-called “memory leak,” in which a programmer forgets to release


memory.


Contents

|

Prev

|

Next
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.