• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private vs Public

 
Dave Morley
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Morley
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic