aspose file tools*
The moose likes Beginning Java and the fly likes Object Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Reference" Watch "Object Reference" New topic
Author

Object Reference

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

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;


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
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 ]

Cheers!
RSR
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ 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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Aug 29, 2008
Posts: 580

@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

Joined: Jun 06, 2007
Posts: 2716
    
    6

<something>.dispaly;


One way is new A().display(); where something is replaced by new A()


SCJP, SCWCD.
|Asking Good Questions|
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11328
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Aug 29, 2008
Posts: 580

Thanks Ernest Friedman-Hill for your help.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object Reference