CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2000
    Location
    Houston Texas
    Posts
    4

    "hidden" argument

    This may be one of most lame questions yet: Can someone explain in the most simplest terms to me how and/or why it would be needed to hide a member variable?
    I have been in the middle of this JAVA class and most makes sense but there are small things like this that I just can't grab. The root of my question comes from the JAVA Tutorial 2nd ed. page 107-108. I read and re-read it and still can't get it. Thanks for your help. b.

    ...newbie to the JAVA world, be gentle.
    I have many questions.

  2. #2
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    22

    Re: "hidden" argument

    I don't have the Java Tutorial book, but I'll try and give an idea as to why you hide variables..

    It comes from the idea of object oriented programming, you should be able to build an entire program by just placing together objects and not directly modifying any variable/structure inside of the class. So, say you wanted a Binary Tree structure, you should be able to just import it and then create it and start working w/ it right away not worrying about what is going on behind the scenes.
    So if the user doesn't need to know what the variable does, or if the user changing it at the wrong time would screw it up, it is hidden. And if you want to allow a user to change the variables value, you usually have functions to get the current value, and then a function to set it. Take a look at the java api docs and you will notice that they have a function to get and to set the value of almost all variables needed to be set/queried by the user.


    "There are no facts, only interpretations."
    -Friedrich Nietzsche

  3. #3
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: "hidden" argument

    Well, basically OOP recommends to hide all the variables in a class, but I don't think its restriction. U can just declare public variable and access it from other classes.

    But now consider the scenario which happends in real projects...
    1. In the begining of a project you develop a class with public variable.
    2. You access it from other classes.
    3. As you project goes on you will end up having 100/1000 classes accessing the variable directly.
    4. No harm till now.
    5. Now if your project requirement says you to modify another variable based on changes on that 'public' variable, what will you do?
    6. You will have no choice but to have code to update the other variable everywhere you used that 'public' variable.

    It is called unmaintainable code where you have to change all those 100/1000 classes just for a small change.

    This is just view-point. Another view point is when this variable is accessed by more than one thread. In multi-threaded environment, two threads might create race condition (two of them try to change it simultaniously).

    So the main reason to "hide" those variables is...

    1. To keep your code maintainable for future (and mostly unexpected) requirements in project.

    2. To take care of race condition (like synchronising access on the variable using 'synchronized' keywork to methods changing or accessing that variable).

    Most importantly there is a very important concept in OOP called 'Object Responsibility'. It is responsibility of a 'class' to keep all its internal attributes in valid state, and the best way to do is control the access to the variables with 'methods'.

    Hope you got what I want to say.

    - UnicMan
    http://members.tripod.com/unicman

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured