• 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

instance withput reference

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..!!!
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
type it in and compile it
 
jignesh soni
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
jignesh soni
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to have duplicated your last post. Did you have something to add?
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic