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:
A: Private method access
Posted by John Largo on May 17, 2000 at 3:01 PM
The main method is not a class client, it's a top-level nested method, which is a member of the CountInstances. As such it has full access to all private members. > > 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"); > > } > } >
Replies:
|