"Thinkinginjava" 8, object initialized to NULL, base type default initialized to 0

Source: Internet
Author: User

/*** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.