This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Private access method
Posted by chandra sekaran on May 19, 2000 at 6:25 AM
> > In the following code, a private member function (getNumInstances) is being called by the class's > client. I thought private member functions are > only accessible to class's member functions and > not the external world. Please explain. > > public class CountInstances { > private static int numInstances = 0 ; > > private int getNumInstances() { > return numInstances; > } > > private static void addInstance() { > numInstances++; > } > > CountInstances() { > CountInstances.addInstance(); > } > > public static void main(String args[]) { > CountInstances ref = new CountInstances(); > System.out.println("Starting with " + ref.getNumInstances() + " Instances"); > > } > } > private methods can be accessed only by the other methods of the same class. The object "ref" is a instance of CountInstances class so it has the access.
Replies:
- answer quinith May 02, 2001 at 4:46 PM
(0)
|