Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Property of an Object in Java

 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created two seperate classes as :-

Movie.java


MovieLibrary.java


Now my question is that i have used only 1 instance(or Object in general) of the class 'm' in "MovieLibrary.java" class for displaying two different sets of values.
Movie m = new Movie();
m.movieTitle="Bhaagbaan";
m.movieGenere="Drama";
m.movieRating=5;



m.getMovieInfo();

m.movieTitle="Mother India";
m.movieGenere="Drama";
m.movieRating=5;

m.getMovieInfo();


I thought for every new values we have to create different objects like in this case 2. Does it right ?
When we have to create multiple objects and when we can have single object, please explain breifly ?

Thanks in advance...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your member variables are not private, they can be changed by anyone, anywhere. so when you do this:

m.movieTitle="Mother India";

you are CHANGING the value. You are effectively erasing the value of "Bhaagbaan" and sticking in the new value of "Mother India". You can't get the value "Bhaagbaan" back again.

If you want to store data for two independent movies, you would need to create a new, second object.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Fred said. If you don't *need* the previous movie, then it doesn't really matter if you create a new one or re-use an old one.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:
I thought for every new values we have to create different objects like in this case 2. Does it right ?
When we have to create multiple objects and when we can have single object, please explain breifly ?


Here, you have changed the state of the single instance.And, it's depends on your requirements, whether, to create a new instance or change the existing object's state!
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m is a reference to a Movie object. When I mean a reference, it means it contains the actual address location of the object of Movie class.

Let me explain with an example. Suppose, you own a house. If you wish to call a painter to paint the house, you will go and give him the address of your house - may be write it down on a piece of paper. This piece of paper is a reference and the House is the object. Now this painter uses the address to come to your house and paint it. Suppose you paint your house red this time. Now again, if you call the same or different painter by giving him the same address. Now when this painter paints your house - the color of your house will change from red to green - the red color is lost.

This is what happens in your case. When you change the name from Baghbaan to Mother India - the value is changed for the same object and hence Baghbaan is lost.

As said by David - the change depends on your need. If you wish to change the color of YOUR house, you will give the same address again and again i.e. use the same object. But if your friend's house is in need of some painting - you have to create a different object.
 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you David and Sonia with your practical example of painting a house. I have got my answer.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome Vijay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic