Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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 ????

Answers were Sorted based on User's Feedback



I am newbie in J2EE & looking for strong basic behind each concept as, Why do we use private ..

Answer / mmr

procedural oriented:-
int i=0;
int j=0;
int showi(){
print i;
}

void main(){

i++;// using i var with out knowing the intention of its use
could leads to unexpected behaviour
showi();
}

Object oriented:-

class mydata{
private int i=0;
int j=0;

private showi(){
print i;
}

void main(){
i++; // not allowed as i can be accessble only thru the
//member function that means the data will be operated thru
//the method exposed to access it which are private to the
//class

showi();
}

Is This Answer Correct ?    0 Yes 0 No

I am newbie in J2EE & looking for strong basic behind each concept as, Why do we use private ..

Answer / 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

I am newbie in J2EE & looking for strong basic behind each concept as, Why do we use private ..

Answer / srinivaskumar.nimmana

Here data is very important, data means variable.. if our class variable are need access by several classes because of that classes code may corrupt our classes code because of our data directly available to that classes, so we need secure to our data that's why we declare as private variables

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More J2EE Interview Questions

What is the container?

0 Answers  


What is java 2 platform, enterprise edition (j2ee)?

0 Answers  


What is business logic?

0 Answers  


Give some advantages of orm (object-relational mapping)?

0 Answers  


Why do we use DAO?

3 Answers   Accenture, Tech Mahindra,


What software is needed for java programming?

0 Answers  


What is jlayeredpane java?

0 Answers  


What is the best java application server?

0 Answers  


What are web technologies in java?

0 Answers  


What is a java applet?

0 Answers  


What is core java and j2ee?

0 Answers  


can someone tell me how does preparedstatement works? its an interface and where is the implementation?

3 Answers   Satyam, TCS,


Categories