首页 > 编程语言 > 详细

Java:thinging in java p158 exercise 1

时间:2015-04-19 11:31:13      阅读:227      评论:0      收藏:0      [点我收藏+]

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

 

Java:thinging in java p158 exercise 1

原文:http://www.cnblogs.com/taoxiuxia/p/4438783.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!