HomeC# / JavaWhat is the Difference between Public and Private variables?

What is the Difference between Public and Private variables?

  • Print
  • E-mail
User Rating: / 0
PoorBest 

A couple of days ago, a friend of mine asked me the question, what is the difference between public and private variables? As I was answering his question I thought, hey that would be a good topic for an article. On this article I'll share my answer to his question and expand a little further on this topic.

On object-oriented programming languages like Java and C#, you'll frequently come across variables with the keywords Public and Private used on variable declarations.

public int x;
private string name;

First of all, the keywords Public and Private are part of what is called Access Level Modifiers in Java, and simply Access Modifiers in C#. What they do is to restrict access to a variable depending on what access modifier it has. For this article, let's just call them Access Modifiers.

There are actually more than just two Access Modifiers for both Java and C#. Public and Private are just the common ones we see in normal everyday programming. To answer the question, the difference between Public and Private variables is that Public variables are variables that can be used outside of its own class. Private variables can be used only inside its own class. Take a look at the sample code I provided below.


1      class Person {
2         public String name = "John Doe";
3         private int age = 20;
4      }
5      
6      class SampleApp {
7         public static void main(String[] args) {
8            Person john = new Person();
9            String personName = john.name;
10           int personAge = john.age;
11        }
12     }

The example above is for a Java application though with only a few minor changes you can turn it into a C# application as well. Anyway on to the explanation. If you try to compile this code, you'll get an error at Line 10. You got a compile error at Line 10 because you were trying to access a private variable belonging to another class. In this case, you were trying to access the private variable private int age with the command john.age;. As I said in the previous paragraph, Private variables can only be used by or in its own class. Class SampleApp being another class simply is not allowed to access a private variable from another class. On the other hand, the Public variables are accessible to any other other class that wants to use it. As you can see, Line 9 didn't cause any compile error at all.

Having answered my friend's question, he sort of hinted on slapping all variables with the keyword Public to make it all accessible to other classes. I told him it was bad design to make all your variables have the public access modifier. That is another good topic to write an article on, so wait for that sometime in the future. Before I end this article though, I got a couple of closing pointers regarding access modifiers.

  • Setting a variable to public doesn't only mean that you can read that variable's value from another class, it also means you can change that variable's value from another class. That might not be your intention and it can lead to some disastrous results when someone messes with a public variable.
  • Only make your variables public if you need to, keep your variables private as much as possible. If you need to get a private variables value, make an accessor method for it. That is, use Getter and Setter methods in Java, and use Properties in C#.
  • When extending or subclassing a parent class, your subclass will not inherit the private variables of the parent class. They will inherit the public variables of the parent class, but not the private variables. If you want a subclass to inherit a private variable, you should change that variable's access modifier from private to protected. Protected means your variable will be accessible to its own class, package (in Java), namespace (in C#) and subclasses, but not to other outside classes.

I hope this article did answer the question regarding the difference between public and private variables. If you have any more questions, please do comment and I will try to answer your questions as soon as I can. Cheers!

Last Updated on Saturday, 31 October 2009 03:41

 

Polls

What Do You Think About Dragon Age: Origins' AI Combat Tactics System?

Tell A Friend

SocialTwist Tell-a-Friend

Advertisement