wood burning stoves*
The moose likes Beginning Java and the fly likes public/private code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public/private code" Watch "public/private code" New topic
Author

public/private code

Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
Peace & Greetings to all....

I have been studying Java for about 2 weeks and I have to say that being a network guy, I love it so far. I am currentyl reading "Java 2: A Beginner's Guide" (Herbert Schildt). However, under the section "Encapsulation", the author is explaining in terms of how within an object, code or data (or both) may be 'private' or 'public' to the object. The author also mentions that private code cannot be accessed by a piece of the program that exists outside of the object.

My question is, what is meant by a piece of the program existing 'outside' of the object? He sort of lost me on that statement.

Thanks in advance.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

Objects talk to each other all the time. Something as simple as

has the object holding this code referencing System's public variable called out, which happens to be an output stream of some kind. Then we talk to out and call its public println() method. So your code is "outside" of System, and whatever stream object that out refers to.

Right now, go to the JavaDoc for System to see how it documents its public out variable. Then follow the link to the type for out and see its public println() method.

System probably does a lot of other stuff that it keeps private. We don't even see the private methods and fields in the doc.

Does that make sense?
[ August 11, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
Thanks for your reply Stan.....

It sort of makes sense, but i'm still not grasping the concept fully. I'll keep at it until it clicks.


Henry.....

Sorry about not following the guidelines.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Originally posted by Derek Harper:
Thanks for your reply Stan.....

It sort of makes sense, but i'm still not grasping the concept fully. I'll keep at it until it clicks.


Well, let's look at some more code...



In this case, we have a class that has two methods, one public and one private... Now you want to call the methods of this class. Say you have this code.



The call to myMethod1() will definitely work -- that method is public and can be called by anyone. The call to myMethod2() will depend on where the calling code is.

If this code is in a method of the MyClass class, then it will work. Otherwise, it will fail to compile because the myMethod2() is private and can't be called by code "outside" of the MyClass class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Harpo:
The author also mentions that private code cannot be accessed by a piece of the program that exists outside of the object.


This is not fully correct - other objects of the same class can access it.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Your classes and objects have a "public" face and a "private" heart.
The "public" face, which means those bits declared as "public" (there are other forms of access too), is available to any Java programming which happens to get its hands on your class. The remainder is kept hidden from the outside world (though as Ilja Preuss told you, private members of an object are accessible to other objects from the same class).
If you look at the javadoc for any class in the API, you see its "public" face (and you may see some non-private bits too).
Look at this little class:- You will have to produce a Person class and a BankAccountTest class to use this class, but notice:-

[list]You don't have direct access to the "balance" or "holder" fields; if you want to change the holder's address you would have to write[/list]
  • You don't have access to the setBalance method. Try invoking the method as suggested in the Javadoc, and your compiler will refuse.
  • Try compiling the javadoc (use the command "javadoc BankAccount.java" on command prompt).

  • You won't even see the three private members.
  • The details about how the methods work are kept hidden. Nobody needs to know how you calculate the balance after a withdrawal, they just need to know it works.
  • If you change the body of any of your methods, or change a variable so "balance" is changed to "savingsAmount," nobody else will notice any difference.

  • I hope that has helped you. CR
    Derek Harper
    Greenhorn

    Joined: Aug 02, 2006
    Posts: 25
    Thanks guys....I think it's starting to sink in.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38519
        
      23
    I think it's starting to sink in.
    . . . and we are glad to have been able to help.
     
    Consider Paul's rocket mass heater.
     
    subject: public/private code