Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Reference

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class A
{
A()
{}

void display()
{
System.out.println("IN DISPLAY");
}
}


If i create a object by the statement

new A();

Is it possible for me to invoke the display method???
By saying

<something>.dispaly;
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean without saying

A a = new A(); ?

If you just say "new A()", without storing the result, then no, there's no way to get to that object; it immediately becomes eligible to be garbage collected.
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m not sure if you are talking about access modifiers like public, private etc. Assuming so...

Yes you can provided the invoker is in the same package as the invoked. This is 'default' (or otherwise 'package-level') access.
[ November 10, 2008: Message edited by: rakesh sugirtharaj ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Ernest Friedman-Hil

If the object is ready for garbage collection....
I have seen in few programs similar statements are use used....
So what is the point of having such a object??
How can we use such a object??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your own particular class "A", the object won't do anything, and you can't use it, and it will get thrown away. But for some other different classes, this doesn't have to be the case. For example, imagine if the constructor did something a little more interesting? For example,



Given this class, using just "new A()" in your code will create a Thread which runs in the background, doing something amazing. Threads are connected to the JVM via their ThreadGroup and their native code, and so aren't garbage collected as long as they're running.

There are lots of other possibilities. The constructor could store a reference to the just-created object in a list somewhere. The class could extend JFrame, and thus be an instance of a GUI window; visible windows are "attached" to the JVM through the GUI toolkit and can't be garbage-collected. Or the constructor could itself simply do some useful work (open a file, process the data) and so after the constructor runs, the object isn't needed anymore.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ernest Friedman-Hill

the constructor could itself simply do some useful work (open a file, process the data) and so after the constructor runs, the object isn't needed anymore.

So you mean to say that once the constructor is invoked and the code in it starts executing it does not matter if the object which invoked the constructor is grabge collected.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<something>.dispaly;


One way is new A().display(); where something is replaced by new A()
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
You mean without saying

A a = new A(); ?

If you just say "new A()", without storing the result, then no, there's no way to get to that object; it immediately becomes eligible to be garbage collected.

Couldn't you technically do this:

new A().display();

I believe this would create the A object, call the method, and THEN make A eligible for gc. If you don't call display by chaining it to the constructor call, then no, you can't call it later. but
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Tharakan:

So you mean to say that once the constructor is invoked and the code in it starts executing it does not matter if the object which invoked the constructor is grabge collected.


An object won't be garbage collected if any running code has a reference to it; if a method of an object is executing, that code defnitely has a reference to the object. So while that constructor is running, or while any other thread started by that object containing a reference to the object is running, or while any array or collection contains a reference to that object, the object won't be collected.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit and Fred --

Yes, of course -- but I think what we're talking about is that James has seen code of the form

new A();

and was wondering when this would ever be useful.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill for your help.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic