aspose file tools*
The moose likes Beginning Java and the fly likes Private vs Public Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private vs Public" Watch "Private vs Public" New topic
Author

Private vs Public

Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
Good Day

I am currently doing a correspondence course and I am wading through some exercises. If someone could just explain the following to me.

example code:


public class Calculator extends Fram implements ActionListener
{
private Button keysArray[];
private TextField lcdField;
etc...........

public Calculator()
{


The question is this : why would you sometimes decalre the variables i.e. lcdField as private and sometimes declare it as public??? What do you achieve by doing it either private or public.

I just can't seem to get my head around this, I understand that it has to do with the scope of the variable, but in real terms what does it achieve once the program is compiled and running???

I hope I have made it clear enough.

many Thanks
Dave Morley


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

The question is this : why would you sometimes decalre the variables i.e. lcdField as private and sometimes declare it as public??? What do you achieve by doing it either private or public.


If you declare a variable as "private" to the Calculator class, it can only be accessed by objects that is of the type Calculator class. If you declare the variable as "public", then it can be accessed by objects of any type. (There is also the "protected" declaration)

Anyway. Why would you want to do this? The "private" keyword makes the class restrictive and only makes it harder to code, since you don't have all the information available to the rest of the program, right?

Actually, you want to have as little public variables and methods exposed as possible? Why?... Let's say you need to add functionality to the calculator class, and you need to change some of the variables and methods. For "private" variables, you are sure that it is only accessed by the calculator class. As long as you don't change the signature of the original public interface, it will work with all other code.

If you had made it public, you would have to inform every person that used your calculator class, to look into every usage to make sure that everything still works, because you changed an internal variable.

Hmmm... this explanation turned out to be longer than expected...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
Thanks for the swift reply Henry

So in essence what you are saying is that you should keep the variables as Public???

Regards
Dave Morley
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
No, he's saying the opposite: wherever possible, variables and methods should be private.

In fact, at first you could even write all of them as private and then when you add other classes, make public only the ones that the other classes need to access.
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
Thanks Guys

I've Got it.

It's funny because the course clearly states to make all the varaibles public, and I can't understand why.

But I understand now.

many Thanks
Dave Morley
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
It may be telling you to make them public to make things easier for now, with the intention of making them private and using more methods later. Kind of using public variables, demonstrating the problems they have, and then moving on to the better solution. I have seen several courses like that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private vs Public