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;
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...
Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
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.
Never ascribe to malice that which can be adequately explained by stupidity.
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!
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Soniya Ahuja
Ranch Hand
Joined: Jul 20, 2008
Posts: 83
posted
0
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.
SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
Vinod Vinu
Ranch Hand
Joined: Aug 30, 2009
Posts: 217
posted
0
Thank you David and Sonia with your practical example of painting a house. I have got my answer.
Soniya Ahuja
Ranch Hand
Joined: Jul 20, 2008
Posts: 83
posted
0
Welcome Vijay
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.