H
Hal Vaughan
I've had a few questions on objects, pointers, different instances and such
before. I'm self taught and this is one aspect of OOP that has been the
hardest for me to fully grasp. I think I've finally got the hang of it,
because now I'm looking at objects differently.
That leads to a few questions, to see if I really do "get it." Since
objects are instantiated once and every time they're used in a different
class or method, we're only dealing with a pointer to the one object, then
I'm wondering about these two code snippets:
Vector testVec = new Vector();
MyObject myObj = new MyObject();
//Adding myObj as item 0
testVec.add(myObj);
//Variation #1, used in another method elsewhere:
MyObject testObj = (MyObject) testVec.get(0);
testObj.doSomeMethod();
//Variation #2, also used elsewhere:
((MyObject) testVec.get(0)).doSomeMethod();
//Variation #3, also used elsewhere:
Object testObj = testVect.get(0);
((MyObject) testObj).doSomeMethod();
From what I understand, variations 1 & 3 may add another pointer to the
original myObj, but #2 would not do that -- just access the object
directly. But as I think about it more, I can see why *I* need the other
reference in version #1 to make it easier to follow, but I can also see
that the compiler would just keep track of the reference and not need to
even add another pointer -- just keep using the first one. Is that right?
When those variations are compiled, is there much (or any) difference that
effects size or speed of operation? Or any difference overall by the time
they're compiled?
This group has been most helpful with giving explanations and background on
things like this. I do have a number of reference books, but I'm trying to
fill in the gaps in my understand. Any help on this is most appreciated.
While it doesn't effect whether or not I can get a program to work, it does
help my overall grasp of the language.
Thanks!
Hal
before. I'm self taught and this is one aspect of OOP that has been the
hardest for me to fully grasp. I think I've finally got the hang of it,
because now I'm looking at objects differently.
That leads to a few questions, to see if I really do "get it." Since
objects are instantiated once and every time they're used in a different
class or method, we're only dealing with a pointer to the one object, then
I'm wondering about these two code snippets:
Vector testVec = new Vector();
MyObject myObj = new MyObject();
//Adding myObj as item 0
testVec.add(myObj);
//Variation #1, used in another method elsewhere:
MyObject testObj = (MyObject) testVec.get(0);
testObj.doSomeMethod();
//Variation #2, also used elsewhere:
((MyObject) testVec.get(0)).doSomeMethod();
//Variation #3, also used elsewhere:
Object testObj = testVect.get(0);
((MyObject) testObj).doSomeMethod();
From what I understand, variations 1 & 3 may add another pointer to the
original myObj, but #2 would not do that -- just access the object
directly. But as I think about it more, I can see why *I* need the other
reference in version #1 to make it easier to follow, but I can also see
that the compiler would just keep track of the reference and not need to
even add another pointer -- just keep using the first one. Is that right?
When those variations are compiled, is there much (or any) difference that
effects size or speed of operation? Or any difference overall by the time
they're compiled?
This group has been most helpful with giving explanations and background on
things like this. I do have a number of reference books, but I'm trying to
fill in the gaps in my understand. Any help on this is most appreciated.
While it doesn't effect whether or not I can get a program to work, it does
help my overall grasp of the language.
Thanks!
Hal