File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 3355

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: 3355

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: 3355

Welcome to JavaRanch, Amol Dongare
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between public and private