• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object instantiation

 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49798
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic