aspose file tools*
The moose likes Beginning Java and the fly likes Property of an Object in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Property of an Object in Java" Watch "Property of an Object in Java" New topic
Author

Property of an Object in Java

Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
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...


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10924
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 04, 2009
Posts: 2066

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
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
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
Welcome Vijay
 
Consider Paul's rocket mass heater.
 
subject: Property of an Object in Java
 
Similar Threads
How do I get one program to take information out of another one.
Creating and using exceptions
basic java question...please advice
Finding tab(\t) character in a sample java program.
Duplicate key insertion in a Hashmap