• 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

objects and refrerences : a basic question

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the lines :


I want to know how these lines are implemented, ie which of the following is correct :

1- C "points to" B which "points to" object Dog() OR,
2- C "points to" Dog().

thanks in advance.

PS : I guess that 1 is not a very efficient way because it does not give immediate/direct access to an object. Thus, it can increase the time taken to access an object.



 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how it works,
  • A Dog Reference B is created. [ Dog B ]
  • Reference B is now assigned to a new Dog object created in the heap [ = new Dog() ]
  • A Dog Reference C is created. [ Dog C ]
  • Reference C is now assigned to a Dog object that B points to [ = B ]

  • So finally, the created Dog object has totally 2 references (B and C) referring it. Now, you can access/modify the created Dog object with either B or C.
     
    Marshal
    Posts: 76447
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dog() isn't a "name" for the object; it is a constructor call after the new operator, which creates an object which you later give two names.
     
    F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic