• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assigning method results to a reference variable

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am flipping through the Exception Handling chapter in Head First Java and I've come across some code that I don't quite understand.



How can java assign a result of a reference variable method call to a reference variable of type Foo when an object of type Foo hasn't been created? I know it is a stupid question considering I've gotten this far in the book. I just haven't seen it used like this so far.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do you know it's not defined as something like

this sort of thing is common in what are called Factories. A factory is an object that creates other objects. So, you may have a PizzaParlorFactory (to steal from Head First Design Patterns) that returns PizzaParlors:




 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Fred. Please provide full code snippet.

How can java assign a result of a reference variable method call to a reference variable of type Foo when an object of type Foo hasn't been created?


Further more it could be null also. It's not compulsory that it has to return an object only.
 
Chuck Mondi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book doesn't actually define what doRiskyThing is. It is just a small code snippet.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you can assume reference variable can hold,
1) Null
2) Object of same type as reference type
3) Object of child class of the reference type
4) If reference type is interface then it can hold any object that implements that interface.

What do you say? Am I missing anything?
 
Chuck Mondi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just thought that for a reference variable to hold anything, it must be "newed" up. I'm assuming that in this example, they mean to show that Foo f is being "newed" up in the doRiskyStuff method and is then passed back and assigned to f.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There probably is a 'new' somewhere, but you may never see it. it's common to use code written by other parties that return object references. you won't ever have access to the source code, so you won't know what they're doing.

when you get into interfaces, you may not even know why specific kind of object it is - you'll just know the kind of interface, which is good enough (in fact, it's often PREFERRED).
 
I am going to test your electrical conductivity with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic