At the first glance Java seems to solve many of the problems related to dynamic memory management in C. The garbage collector automatically collects objects which are no longer reachable.
public Class Foo { public int i; public String bar; void someMethod() { Class Foo fooObject = new Foo(); // allocates a Foo object on heap } // and now the fooObject IS NO LONGER REACHABLE // It will be collected at some future time and // returned to the heap. } Foo someOtherMethod() { Class Foo fooObject = new Foo(); return fooObject;// allocates a Foo object on heap } // fooObject is STILL reachable from the caller which // gets the reference to it as a return value }