Java:thinging in java p158 exercise 1

Create a class with a String field that is initialized at the point of definition, and another one that is initialized by the constructor. What is the difference between the two approaches.

 1 class Test{
 2     String s1;
 3     String s2="hello world";
 4     String s3;
 5     Test(){
 6         s3="hello java";
 7     }
 8 }
 9 public class ConstructorTest {
10 
11     public static void main(String[]args){
12         
13         Test t=new Test();
14         System.out.println(t.s1);
15         System.out.println(t.s2);
16         System.out.println(t.s3);
17         
18     }
19 }

输出

null
hello world
hello java

 

posted @ 2015-04-19 11:05  陶修瑕  阅读(149)  评论(0编辑  收藏  举报