This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between public and private Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between public and private" Watch "Difference between public and private" New topic
Author

Difference between public and private

Amol Dongare
Greenhorn

Joined: Dec 27, 2007
Posts: 1
Hi Ranchers,
I want to know the difference between public and private.
Thanks in advance.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
This question has nothing to do with servlets, but in short

private means only visible within the class in which a method of variable is defined.

public means visible for everyone who knows how to find your method, class of variable.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
npk chowdary
Greenhorn

Joined: Dec 09, 2007
Posts: 21
In addition to that private members cannot be inherited.so they are not able to override.but it seems like overriding.check for it.
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
the members private are of class and os members with public are access by all.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by camilo lopes:
the members private are of class and os members with public are access by all.


What are those "os" members here?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

The data which you keep within yourself without revealing outside, is private. Say for example your "salary"

Whereas some information you may reveal without losing anything. for example, your name.

How do you have a control on its visibility? There are certain access modifiers to achieve the same. "private" and "public" are two among those and they help us in providing the control over the variables which represent the data we are interested in securing it.

Private - visible only to the class where it is present. same as your salary is only visible /known to you.

Public - visible to all public. Same as your name, Date of Birth etc.

Does that help?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Welcome to JavaRanch, Amol Dongare
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between public and private
 
Similar Threads
passing assertion from methods(public/private)
Assertion
private/public constructor
difference between protected and private
Inner class access modifiers