I am newbie in J2EE & looking for strong basic behind each
concept as,
Why do we use private access specifier before each variable
of
type property bcoz ultimately to access it outside the class
we use public method as getter & setter ?

If we use getter & setter to access it outside the class so
better we make variable as public so that it is easily
available outside of the class...

Please guide me why do we use as private ????

Answer Posted / arindam ghosh

Hi All,
I think that the access modifiers of Java comes under the
umbrella of encapsulation.If you don't protect your
variable through the access modifiers or if you just make
them final then all the values of those variables can be
changed at any point of time because the variables are used
by many classes as Java is designed to make the code
reusable.You create a variable by using private and
generate the public getter and setter for that
variable.Using this way you can access your variable you
can assign new value in the variable but you can't change
it.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Describe the file types *.ear, * .jar and *.war?

680


What is gridlayout in java?

623


What is event handling in java?

661


What is the difference between session bean and entity bean ?

638


What is a jbutton in java?

687


What are container services?

701


What are different modules in spring?

711


Who generates bytecode in java?

732


What is fatal error?

767


What is source file in java?

673


Describe orm?

709


Define hashtable in j2ee?

712


What is full form of j2ee?

711


Explain the j2ee tiers.

645


What is the J2EE module?

778