View Full Version : To use public or not to use public

01-03-2005, 11:51 PM
Hi, (first post)
I am a little confused as whether to use public instance fields or not. I have to write a program (in JAVA) to simulate playing rummy. I have created a class that holds my deck of cards, and shuffles them and other stuff. I have created a class to play rummy, however the only way i can get it to work is to to make the array that holds the deck of cards in the deck of cards class public. I don't think this is a very good idea as all the text books i read say "DON'T USE PUBLIC". Anyone got any ideas to get around this? I was thinking maybe return the remaining deck of cards to the play rummy class.?


01-04-2005, 01:37 AM
You shouldn't make your data members public. What you do is make some public methods in your class to return the values from those data members. That way nothing outside the class can directly access or modify your data. This is one of the fundamentals behind OOP. You now have an object with methods you can use with other objects.