what is the use of making a variable public.
After all the client is provided only with the .exe code
So now whats the actual use of maling a variable private?

.exe code?
you declare a variable as private to increase encapsulation. also, this makes sure you control how other classes can access the value of the variable. when they're marked private, the value of the variable can only be changed or gotten through the use of methods (setters and getters).

I'm not going into detail here, but you can find tons of examples online where declaring variables as public causes unexpected trouble if wrongly used, because other classes were capable of changing the value of the variable directly, without going through methods.

so do u mean to say that their use is purely for the convinience of the programmer in designing the code ?

If i can use public and design a program carefully(although a bit inconvenient) then can i completely avoid using private?

it's more about making sure the code using the class gets the correct values returned from methods, and such.

you can always declare your variables public, but this will lower the security you have that everything will "work as planned".

you can design your program as carefully as you want, but one of the big up-sides of object oriented design is that the code can be reused. so, whether or not you design the program very, VEEERY carefull .. are you 100% sure no other developer will ever use these classes AND be at least as carefull as you?

big part of choosing the way you design and write your code, is taking into consideration what will happen to it, and how it will be used, once you are done writing the code and handed it over to someone who needs the same classes to get his application to work, or who has to continue working on the application you started working on.

what would happen, (for instance) if you declare a variable as both public and final, and someone tries to alter the value from outside the class? you, as original author of the class know that it's supposed to be final, that it's value can not change after being initially set, so you don't provide a setter. this would surely fix that problem if and only if it was a private variable, but what if it's public, there's no documentation, and the developer using your code doesn't notice the fact that it's a final variable? that's an exception in the making for you :)

If i can use public and design a program carefully(although a bit inconvenient) then can i completely avoid using private?

Apart from all the good reasons stultuske gave, you should also consider that no (real-life) program is ever finished. Requirements change, increase, evolve, operating environments change. Its no enough to "design a program carefully" for currently known requirements. What you have to do is design it carefully to allow growth and evolution in ways you can't yet define... which allowing uncontrolled random access to a class's variables definitely does not help.

well .. I was a little bored, so I wrote a bit of code that will show quite clear what trouble you can run into:

public class PrivateUsage {

    private int numberOne;
    public int numberTwo;
    public final int numberThree = 1;

    public void setNumberOne(int number){       if ( number <= 10 && number > 0 ) this.numberOne = number;}
    public void setNumberTwo(int number){       if ( number <= 10 && number > 0 ) this.numberTwo = number;}

    public int getNumberOne(){      return this.numberOne; }
    public int getNumberTwo(){      return this.numberTwo; }
    public int getNumberThree(){    return this.numberThree; }

    

}

it's quite clear we don't want any of the getters ever to return a value that:
for numberThree: is NOT 1
for the other two: is NOT between 1 and 10 (1 and 10 included)

now, let's say this next main method is in a class that has access to the PrivateUsage class:

public static void main(String args[] ){
        PrivateUsage a = new PrivateUsage();
         // a.numberThree = 5;       compile time exception
        a.setNumberOne(7);
        a.setNumberOne(23);
        // a.numberOne = 23;         compile time exception
        System.out.println("Number one: " + a.getNumberOne());

        a.setNumberTwo(7);
        a.setNumberTwo(23);
        System.out.println("Number two: " + a.getNumberTwo());
        a.numberTwo = 23;
        System.out.println("Number two: " + a.getNumberTwo());
    }

Now, the original developer wanted this output to appear:

Number one: 7
Number two: 7
Number two: 7

but, actually, you get the next output:

Number one: 7
Number two: 7
Number two: 23

since numberTwo is set as public, a.numberTwo = 23 will not create compile time exception, or a runtime exception, so the value of numberTwo will be set to 23, since your input is not passing the validation you've set in the setter method.

My favourites are the ones involving multiple threads - eg
User interface sets public search field vars in Database class and calls "search"
Database class starts to execute database retrieval on separate thread
User clicks more buttons, UI class changes values of search fields
Database class returns info - now obsolete, but nobody knows what's going on.

IMHO accesses to public variables from multiple threads causes the worst kind of inconsistent un-debuggable problems.

long story short, keep your privates private :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.