• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor

 
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.
//: c04:Flower.java
// Calling constructors with "this."
public class Flower {
int petalCount = 0;
String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println(
"Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only, s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
//! this(11); // Not inside non-constructor!
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
} ///:~
Hi friends,
Consider the code segment below from "Thinking in Java"

//: c04:SimpleConstructor.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?
Regards,
Hassan.

 
Author and all-around good cowpoke
Posts: 13078
6
  • 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.
Bill

------------------
author of:
reply
    Bookmark Topic Watch Topic
  • New Topic