File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes objects and references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "objects and references" Watch "objects and references" New topic

objects and references

le taylom
Ranch Hand

Joined: Dec 01, 2000
Posts: 44
Hi Guys,
This is a post to solidify my understanding and hopefully get some discussion comments back.
I have a class, call it MyClass, and I declare a reference variable to an object with class-scope:

Within some other method, methodA, in my class I call methodB, and within methodB I instantiate the reference to myObject. Then from within methodA I call methodC, and use the object (calling its methods etc):

So, is this the most object-oriented efficient way to do this?
Or is the most OO efficient way to do it like so:
Do not declare a reference with class-scope. Instead pass the reference of the object between the methods by copying the refernce into newly created objects:

What are the pros and cons of the two approaches?
Particularly the implications if I make changes to an object reference I have passed into another method etc.....
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

The first method is the best method. BUt its not a question of object orientation or not. there is no need to pass around that variable. But for object orientatino you may have something like this.
I agree. Here's the link:
subject: objects and references
It's not a secret anymore!