Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Simple doubt

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above coding, I have one doubt:

p.getName() --> p is the reference variable holding the Person Object anf thru p we can invoke its method getName();

p.getCar().getName() --> what does this means? How many level of dot we can have. For example: ref-obj.method.??
 
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getCar() method is returning an object
of type car...
U can always use <b>.</b> to call the
methods of this object....
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so, x.y.z means
x - ref variable
y - object
z - method

Any other possibilities??
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p is a reference to a Person object
p.getCar() is a reference to a Car object
p.getCar().getName() is a reference to a String object

In theory, so long as each method returns a reference to an object, you can string any number of "."s together.

In practice, it would be bad coding to put too many - certainly not more than what you've got here. For example, the following clearly does the same :



However, the advantage with splitting the code like this is it enables debugging and maintenance. You can log the value of myCar to ensure it's what you expected it to be, etc.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have as many "dot" levels as you want. You can, for instance do

String carSubstring = p.getCar().getName().substring(5);

... or whatever else you want.

jk
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a fairly well known principle called the Law of Demeter which spells out the best practices for using this kind of multi-level access. In short: don't. If you find yourself using multiple dots in an expression, it's a clue that the code you're writing belongs in some other class.
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic