The following is the code in tij:
Class Soup {private Soup () {} // (1) Allow Creation Via Static Method: Public Static Soup Makesoup () {Return New Soup ();} // (2) Create A Static Object and // Return A Reference Upon Request. // ("Singleton" Pattern): Private static source ps1 = new source (); public static source () {Return PS1;} PUBLIC VOID F ()}}}
Class Sandwich {// Uses Lunch Void f () {new lunch ();}} // only one public class allowed per file: public class lunch {void test () {// can't do this! private constructor: / /! SOUP priv1 = new soup (); Soup priv2 = Soup.makesoup (); SANDWICH F1 = New Sandwich (); SOUP.ACCESS (). F ();}} ----------- - The return value is an object reference for the Soup. Here this class is a self-reference class, and the UTIL package in the JDK is implemented like this. Access is a static method, so you should call directly with the class name. Moreover, Access () returns a SOUP object, so you can access the class method f.
Can't SOUP priv1 = new source (); because the constructor is a private method through the public static source () {RETURN PS1;} is actually equal to the call for the primary constructor SOUP.ACCESS () Returns an object, f ( ) Is a non-private method of SOUP, calling it in an object of SOUP, but it is normal, so: Soup.access (). F ();