//Initialization/constructortest2.java//TIJ4 Chapter initialization, Exercise 2, p158/*Create A class with a String field this is an initialized at the point of* definition, and another one it is Initializ Ed by the constructor. What is* the difference between the approaches.*/classTester2 {String S1; String S2= "Hello"; String S3; Tester2 () {S3= "Good-bye"; }} Public classConstructorTest2 { Public Static voidMain (string[] args) {Tester2 T=NewTester2 (); System.out.println ("T.S1:" +t.s1); System.out.println ("T.S2:" +t.s2); System.out.println ("T.S3:" +T.S3); }}// outT.S1:NULLT.s2:hellot.s3:good-bye
Initializing undefined S3 is overridden by the constructor, that is, the initialization within the constructor overrides the domain initialized by the class. The code comes from a great God of thinking exercises.
The difference between class initialization and constructor initialization