/*** Book: Thinking in Java * Function: Initialize the object to null, the base type is initialized to 0* 1 by default, where the object is defined. * 2, in the constructor of the class * 3, before using these objects, this is called lazy initialization * 4, using the instance initialization * File: bath.java* time: October 12, 2014 15:21:17* Author: cutter_point*/package Lesson7reusingclasses;import static net.mindview.util.print.*;//Reference class Soap{private String s; Soap ()//constructor {print ("soap ()"); s= "constructed";} Public String toString () {return s;}} public class Bath {private String s1= "Happy", s2= "happy!", S3, s4;private Soap castille;private int i;private float TOY;PU Blic Bath () {print ("Inside Bath ()"); s3= "Joy"; toy=3.14f;castille=new Soap (); {i=47;} This initialization, slightly cock public String toString () {if (S4 = = null) s4= "Cutter_point"; return "s1=" +s1+ "\ n" + "s2=" +s2+ "\ n" + "s3=" +s3+ "\ n" + "s4=" +s4+ "\ n" + "i=" +i+ "\ n" + "toy=" +toy+ "\ n" + "castille=" +castille+ "\ n";} public static void Main (string[] args) {Bath b=new Bath ();p rint (b);}}
Output:
Inside Bath () obj1
Soap () obj1
S1=happy
s2=happy!
S3=joy
S4=cutter_point
i=47
toy=3.14
castille=constructed
Obj1
"Thinkinginjava" 8, object initialized to NULL, base type default initialized to 0