File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Public or Private ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Public or Private ??" Watch "Public or Private ??" New topic

Public or Private ??

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Will making public and private objects make a differance on Memory management? or this only useful in encapsulating the objects?
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Access modifiers are only useful for encapsulation purposes. A variable that is declared private and points to some object doesn't want the object it is referencing being garbage-collected... Does it make sense?
Only unreferenced objects may be garbage-collected.
The following threads contains some pointers to garbage-collection and how the latter works:
SCJP Forum: Good resources on Garbage Collection

[Blog] [Blogroll] [My Reviews] My Linked In
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Ideally, the data representation of a class is nobody else's business but its own. That way, a client class wouldn't be tempted to directly make use of the data representation. If the data representation is changed in any way (pretty common thing really) the implementing methods of client classes will be unaffected. So make your instance and static variables (and your internal helper methods as well) private unless you really really must.
Consider Paul's rocket mass heater.
subject: Public or Private ??
It's not a secret anymore!