This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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.
Consider Paul's rocket mass heater.
subject: objects and references
Similar Threads
Creating another Instance of same Session EJB
decision of member variables?
synchronization basics
Exeption Handling