Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    To use public or not to use public

    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.?

    Thanks

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •