It's not a secret anymore!*
The moose likes Beginning Java and the fly likes instance withput reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instance withput reference" Watch "instance withput reference" New topic
Author

instance withput reference

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
public class Exam {
public exam(){
System.out.println("exam constructor");
}
public static void main(String [] args){
new Exam();
}
}

Can we create an object w/o reference like in above example ?

thanks
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Yes, We can create the Object like that,In this simply an Object is created at Heap, No One is Referring that Object, So In your case its become useless after the declaration line....or can say garbage collected..!!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
type it in and compile it


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
thanks sunny,
so if an object is created only to be collected by garbage collector. Then whats the use of creating such an object ? This kind of object is created in mock test and I could not understand the use. Pls explain.

thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Originally posted by jignesh soni:

so if an object is created only to be collected by garbage collector. Then whats the use of creating such an object ? This kind of object is created in mock test and I could not understand the use. Pls explain.


First of all, just because a newly created object isn't assigned to a reference doesn't mean that it is immediately eligble for GC. The construction process may store a reference of itself somewhere.

Second, even if it is immediately eligible for GC, it doesn't mean that it serves no purpose. The construction of the object itself may have a side effect that is desired.

Of course, you can create an object that is immediately eligible for GC, and have no useful side effects -- but how would the compiler know that? So obviously, the compiler can't disallow it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It's always possible to write code that has no use. Especially on mock exams. This is one such example. It's possible that some objects might do something useful in their constructor, and then you don't need the object for anything else after that... but in general that seems like poor programming, and I can't think of any realistic example of this.


"I'm not back." - Bill Harding, Twister
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
Thaaks friends,
so you mean, if I create an object exam without reference like one in example. Then it will printout whatever is in constructor and then it will be callected by garbage collector. But still, I get the printout , right ?
In that sense, whatever process in the constructor is there will be carried out, right ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes.
Krep Lock
Ranch Hand

Joined: Sep 19, 2006
Posts: 43
well this question made me stop and think. i've created objects without storing a reference plenty of times, like in the main method of the Minesweeper clone i wrote after my first semester of java:



this calls the constructor for the gui and ultimately some event listeners get registered, etc etc. but now this topic has me questioning why that unreferenced Minesweeper() object isn't GC'd? the GC must see referenced objects inside and so keeps its dirty paws off, eh?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The JVM can't collect the object until it's done at least running the constructor. Some programs do all their work in the constructor. I don't recommend this style, but it's possible.

Even after the constructor completes, it's possible the object may be ineligible for GC because something in the constructor made the object reachable by other threads. In the case of GUI components, this commonly happens because when you call setVisible(true), the object is registered with the event dispatch thread so that people can click on it and perform other operations. This event dispatch thread will keep a reference to the GUI component until someone calls dispose() on the object - or System.exit(), of course. Many GUI programs do this implicitly by calling setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE) or setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE). Otherwise your program just keeps running.
[ January 28, 2008: Message edited by: Jim Yingst ]
Krep Lock
Ranch Hand

Joined: Sep 19, 2006
Posts: 43
well this question made me stop and think. i've created objects without storing a reference plenty of times, like in the main method of the Minesweeper clone i wrote after my first semester of java:



this calls the constructor for the gui and ultimately some event listeners get registered, etc etc. but now this topic has me questioning why that unreferenced Minesweeper() object isn't GC'd? the GC must see referenced objects inside and so keeps its dirty paws off, eh?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You seem to have duplicated your last post. Did you have something to add?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Also, keep in mind that the constructor may cause an external reference to reference the object -- for example, if minesweeper is a listener which is added the GUI, there is a reference from the GUI system. Hence, the object can't be collected.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instance withput reference
 
Similar Threads
A question about casting.
Inner classes referencing outer class name?
Exam Release Date?
about default constructor
starting preparation for SCJP