• Post Reply Bookmark Topic Watch Topic
  • New Topic

objects and refrerences : a basic question  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.



 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
     
    Campbell Ritchie
    Marshal
    Posts: 55761
    163
    • Mark post as helpful
    • send pies
    • 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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!