| Author |
object instantiation
|
Randy Smith
Ranch Hand
Joined: Mar 27, 2011
Posts: 44
|
|
Appreciate if anyone can explain this to me. I would like to access methods of another object, do I instantiate that or I access it without instantiating it.
E.g.
Is there any difference, I'm really confused about this. I've always assume that you cannot access another object methods without first instantiating that object with "new". Looking at some code samples...it looks like they do the same thing?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
You don't have to create the object, but somebody has to. If you just declare a variable like
then there's no object to use; you'll get a NullPointerException trying to call a method using this variable. Somebody, someplace, has to create an object, and you have to assign the variable a to point to it, before you can call an instance method using a. It might not always be obvious who is creating an object -- for example, if you call String.substring(), a new String is being created, even though you don't see new being used (the code for substring() is using it, though) -- but somebody has to do it.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Randy Smith
Ranch Hand
Joined: Mar 27, 2011
Posts: 44
|
|
Ernest Friedman-Hill wrote:You don't have to create the object, but somebody has to. If you just declare a variable like
then there's no object to use; you'll get a NullPointerException trying to call a method using this variable. Somebody, someplace, has to create an object, and you have to assign the variable a to point to it, before you can call an instance method using a. It might not always be obvious who is creating an object -- for example, if you call String.substring(), a new String is being created, even though you don't see new being used (the code for substring() is using it, though) -- but somebody has to do it.
Thank you for responding! For clarifications, do you meant this?
B can use A's methods because it has been instantiated by C
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
Randy Smith wrote:
B can use A's methods because it has been instantiated by C
No; the variable "a" in class "C" and the variable "a" in class "B" are separate and unrelated; B's "a" remains null. But something like this, on the other hand:
Code in class "A" creates an instance of "A" and stores it in the static variable "theA". Class "B" uses that "A" object; it doesn't need to use "new" itself.
|
 |
Randy Smith
Ranch Hand
Joined: Mar 27, 2011
Posts: 44
|
|
Ernest Friedman-Hill wrote:
Randy Smith wrote:
B can use A's methods because it has been instantiated by C
No; the variable "a" in class "C" and the variable "a" in class "B" are separate and unrelated; B's "a" remains null. But something like this, on the other hand:
Code in class "A" creates an instance of "A" and stores it in the static variable "theA". Class "B" uses that "A" object; it doesn't need to use "new" itself.
Is using and creating static object poor programming?
|
 |
Randy Smith
Ranch Hand
Joined: Mar 27, 2011
Posts: 44
|
|
Randy Smith wrote:
Ernest Friedman-Hill wrote:
Randy Smith wrote:
B can use A's methods because it has been instantiated by C
No; the variable "a" in class "C" and the variable "a" in class "B" are separate and unrelated; B's "a" remains null. But something like this, on the other hand:
Code in class "A" creates an instance of "A" and stores it in the static variable "theA". Class "B" uses that "A" object; it doesn't need to use "new" itself.
Is using and creating static object poor programming?
I guess not http://www.developer.com/tech/article.php/626421/Static-Initializers-and-Lazy-Instantiation.htm
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
References marked static have their uses, but they are a special case where there is one instance per class.
You can have an example where you pass an A instance like thisNow somebody can pass the a and you are making use of its fooFoo() method.
|
 |
 |
|
|
subject: object instantiation
|
|
|