构造函数是静态方法吗

xingyezhicna2 2012-09-28 04:19:07
think in java96页上说即使没有显示的使用static关键字,构造器实际上也是构造方法。但是我觉得为什么构造器还可以用this,还可以访问成员变量。
新手发帖,求高手解答
...全文
513 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingyezhicna2 2012-09-28
  • 打赏
  • 举报
回复
这个正在看呢
龙四 2012-09-28
  • 打赏
  • 举报
回复
这个答案比《thinking in java》的作者权威多了,没人信?


[Quote=引用 11 楼 的回复:]

看这篇jvm大牛的博客:http://rednaxelafx.iteye.com/blog/652719
[/Quote]
xingyezhicna2 2012-09-28
  • 打赏
  • 举报
回复
嗯,我再看看
xingyezhicna2 2012-09-28
  • 打赏
  • 举报
回复
Even though it doesn't explicitly use the static keyword, the constructor is actually a static method. So the first time an object of type Dog is created, or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the classpath.

《java编程思想》中文第四版96页: 翻译如下:

总结一下对象的创建过程,假设有个名为Dog的类:

1.即使没有显示地使用static关键字,构造器实际上也是静态方法。因此,当首次创建类型为Dog的对象时(构造器可以看成

是静态方法),或者Dog类的静态方法/静态域首次被访问时,Java解释器必须查找类路径,以定位Dog.class文件
龙四 2012-09-28
  • 打赏
  • 举报
回复
看这篇jvm大牛的博客:http://rednaxelafx.iteye.com/blog/652719
soton_dolphin 2012-09-28
  • 打赏
  • 举报
回复
The constructor is an unusual type of method because it has no return value. This is distinctly different from a void return value, in which the method returns nothing but you still have the option to make it return something else. --原文
xingyezhicna2 2012-09-28
  • 打赏
  • 举报
回复
这可是think in java,不知道他为什么这么说
soton_dolphin 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
但是书上怎么说是不加static的静态方法,难道错么吗
[/Quote]

你就记住构造器就是构造器,不要和什么静态方法作比较了,每个人对构造器的理解不同,书上只是作者的理解
xingyezhicna2 2012-09-28
  • 打赏
  • 举报
回复
翻译应该不是问题,都出第四版了,话说我也感觉区别很大,但是不知道书上为什么这么说,像确认一下
hailhua 2012-09-28
  • 打赏
  • 举报
回复
不知道是翻译的问题还是你断章理解的问题

静态方法和构造方法意义和作用不同,而且使用方式也不同。--你自己可以比较一下

等到你使用java一段时间后就明白了
xingyezhicna2 2012-09-28
  • 打赏
  • 举报
回复
但是他为什么还可以用this呢,不是静态函数不能用吗
  • 打赏
  • 举报
回复
调用机制决定的,那你说为什么main就入口,我其他函数不行
xingyezhicna2 2012-09-28
  • 打赏
  • 举报
回复
但是书上怎么说是不加static的静态方法,难道错么吗
soton_dolphin 2012-09-28
  • 打赏
  • 举报
回复
constructor 就是一个特殊的没有返回类型的method
xingyezhicna2 2012-09-28
  • 打赏
  • 举报
回复
....,难道问题太幼稚了,没人回家。。。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧