Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven


Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends.
Consider the code below from "Thinking in Java".I have two confusions in this code.
// Calling constructors with "this."
public class Flower {
int petalCount = 0;
String s = new String("null");
Flower(int petals) {
petalCount = petals;
"Constructor w/ int arg only, petalCount= "
+ petalCount);
Flower(String ss) {
"Constructor w/ String arg only, s=" + ss);
s = ss;
Flower(String s, int petals) {
//! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
Flower() {
this("hi", 47);
"default constructor (no args)");
void print() {
//! this(11); // Not inside non-constructor!
"petalCount = " + petalCount + " s = "+ s);
public static void main(String[] args) {
Flower x = new Flower();
} ///:~
Hi friends,
Consider the code segment below from "Thinking in Java"

// Demonstration of a simple constructor.
class Rock {
Rock() { // This is the constructor
System.out.println("Creating Rock");
public class SimpleConstructor {
public static void main(String[] args) {
for(int i = 0; i < 10; i++)
new Rock(); // line A

In the above code i have some confusions...
1:Bruce Eckel says " new Rock(); " at line A "creats an object
and storage is allocated and the constructor is called"
However, in my opinion new always creats a "New Object", but
how memory will captured or allocate without an "instance variable.
I mean if there is no "instance variable" then how it
is possible for object to occupie space in mempry i.e
it will take "Zero Bytes" in memory if it has no variable.
This means that object is not created!
2: Another thing is that, at " line A " we have not get the "reference"
of the object created by "new" and what i have learned is that if our programme
lost the reference of the object then that object is available for GC.What about
the objects created in above code?.Does they are available for GC?

Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. I already answered that once. Even the simplest Object gets some memory allocated for various internal purposes. Try creating a few thousand Object in an array, check memory before and after.
2. As you observed, creating an object without keeping a reference generally makes the object immediately available for GC. However, some object creation keeps a reference in a non-obvious way. For instance:
new Thread( this ).start() ;
There is now a reference in the ThreadGroup for the Thread that executed the statement.

author of:
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic