• Post Reply Bookmark Topic Watch Topic
  • New Topic

object and object reference  RSS feed

 
Madhawi Maurya
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please tell me the difference between object and object reference in java. How can we use them in a program? Pleses also give an example.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


An object in Java in an instance of a class, usual (but not always) created with the new statement as shown above.

The varaibale myArrayList is an object reference to the ArrayList (object) we created with the new statement.

The object reference allows us to manipulate the objectw e created from the class, we can call its methods, and pass its reference to other classes and methods.

I would suggest that you go to the Java site and check out the introductions etc, it will help, I am always learning new stuff about the language and I have been using for about 7 years.

have fun, and enjoy your coding

G
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to carry this a bit further, if we add this line ...

we make another reference to the same object instance. We can have lots of references to one instance. And with this line...

we pass a COPY of the REFERENCE to the other method. The other method can see and modify the ArrayList instance, but it cannot modify our myArrayList variable to point to a different ArrayList.

As you learn more about Java, pay attention to the life cycle or "scope" of your variables. When all references to an object go out of scope then the instance becomes eligible for Garbage Collection.

Hope that helps!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhawi Kr, please check your private messages. You can see them by clicking My Profile.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!