Javajava中无参构造方法法的作用

15:35 提问
Java中一个只实现了接口的类,在它的构造方法中调用了super()方法,这样做有什么意义呢
今天在看spring源码spring-core包中的EncodedResource.java时发现它的其中一个构造方法里出现了:
private EncodedResource(Resource resource, String encoding, Charset charset) {
Assert.notNull(resource, "Resource must not be null");
this.resource =
this.encoding =
this.charset =
一开始并不知道super()到底做了什么,后来经别人提醒知道了原来这是在调用Object类的
的构造方法,这样是对的吗?还有,这样做有什么意义吗?
回复数排序
所有类的父类为Object
当你实例化的时候,在调用该类的构造方法的时候,首先都会调用父类的构造方法。
super。调用父类的构造方法。也可以调用父类的方法
如果不写构造方法,默认会提供一个无参构造方法,手写重载构造方法,必须第一行调用super,对于这个语法现象的理解我觉得 是在做类的内存初始化时(比如new)需要先初始他父类的方法和属性
此处附上源代码:
EncodedResource.java
the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
package org.springframework.core.io.
import java.io.IOE
import java.io.InputS
import java.io.InputStreamR
import java.io.R
import java.nio.charset.C
import org.springframework.core.io.InputStreamS
import org.springframework.core.io.R
import org.springframework.util.A
import org.springframework.util.ObjectU
Holder that combines a {@link Resource} descriptor with a specific encoding
or {@code Charset} to be used for reading from the resource.
Used as an argument for operations that support reading content with
a specific encoding, typically via a {@code java.io.Reader}.
@author Juergen Hoeller
@author Sam Brannen
@since 1.2.6
@see java.io.Reader
@see java.nio.charset.Charset
public class EncodedResource implements InputStreamSource {
private final R
private final S
private final C
Create a new {@code EncodedResource} for the given {@code Resource},
not specifying an explicit encoding or {@code Charset}.
@param resource the {@code Resource} to hold (never {@code null})
public EncodedResource(Resource resource) {
this(resource, null, null);
Create a new {@code EncodedResource} for the given {@code Resource},
using the specified {@code encoding}.
@param resource the {@code Resource} to hold (never {@code null})
@param encoding the encoding to use for reading from the resource
public EncodedResource(Resource resource, String encoding) {
this(resource, encoding, null);
Create a new {@code EncodedResource} for the given {@code Resource},
using the specified {@code Charset}.
@param resource the {@code Resource} to hold (never {@code null})
@param charset the {@code Charset} to use for reading from the resource
public EncodedResource(Resource resource, Charset charset) {
this(resource, null, charset);
private EncodedResource(Resource resource, String encoding, Charset charset) {
Assert.notNull(resource, "Resource must not be null");
this.resource =
this.encoding =
this.charset =
Return the {@code Resource} held by this {@code EncodedResource}.
public final Resource getResource() {
return this.
Return the encoding to use for reading from the {@linkplain #getResource() resource},
or {@code null} if none specified.
public final String getEncoding() {
return this.
Return the {@code Charset} to use for reading from the {@linkplain #getResource() resource},
or {@code null} if none specified.
public final Charset getCharset() {
return this.
Determine whether a {@link Reader} is required as opposed to an {@link InputStream},
i.e. whether an {@linkplain #getEncoding() encoding} or a {@link #getCharset() Charset}
has been specified.
@see #getReader()
@see #getInputStream()
public boolean requiresReader() {
return (this.encoding != null || this.charset != null);
Open a {@code java.io.Reader} for the specified resource, using the specified
{@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}
@throws IOException if opening the Reader failed
@see #requiresReader()
@see #getInputStream()
public Reader getReader() throws IOException {
if (this.charset != null) {
return new InputStreamReader(this.resource.getInputStream(), this.charset);
else if (this.encoding != null) {
return new InputStreamReader(this.resource.getInputStream(), this.encoding);
return new InputStreamReader(this.resource.getInputStream());
Open a {@code java.io.InputStream} for the specified resource, ignoring any
specified {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}.
@throws IOException if opening the InputStream failed
@see #requiresReader()
@see #getReader()
public InputStream getInputStream() throws IOException {
return this.resource.getInputStream();
public boolean equals(Object other) {
if (this == other) {
if (!(other instanceof EncodedResource)) {
EncodedResource otherResource = (EncodedResource)
return (this.resource.equals(otherResource.resource) &&
ObjectUtils.nullSafeEquals(this.charset, otherResource.charset) &&
ObjectUtils.nullSafeEquals(this.encoding, otherResource.encoding));
public int hashCode() {
return this.resource.hashCode();
public String toString() {
return this.resource.toString();
InputStreamSource.java
the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
package org.springframework.core.
import java.io.IOE
import java.io.InputS
Simple interface for objects that are sources for an {@link InputStream}.
This is the base interface for Spring's more extensive {@link Resource} interface.
For single-use streams, {@link InputStreamResource} can be used for any
given {@code InputStream}. Spring's {@link ByteArrayResource} or any
file-based {@code Resource} implementation can be used as a concrete
instance, allowing one to read the underlying content stream multiple times.
This makes this interface useful as an abstract content source for mail
attachments, for example.
@author Juergen Hoeller
@since 20.01.2004
@see java.io.InputStream
@see Resource
@see InputStreamResource
@see ByteArrayResource
public interface InputStreamSource {
Return an {@link InputStream}.
It is expected that each call creates a fresh stream.
This requirement is particularly important when you consider an API such
as JavaMail, which needs to be able to read the stream multiple times when
creating mail attachments. For such a use case, it is required
that each {@code getInputStream()} call returns a fresh stream.
@return the input stream for the underlying resource (must not be {@code null})
@throws IOException if the stream could not be opened
@see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)
InputStream getInputStream() throws IOE
不写也是可以的,你可以试一试去掉super,
写不写无所谓,子类构造方法第一行默认是调用父类的无参构造方法的,这里只是显式的将其写出来了。但是调用父类有参构造器时,必须显式的写出super(xxx)。
此处super()无意义,只是将省略的东西,写出来而已。
:这是这个类的有参构造函数,调用父类的构造函数,而且这是java语法规定,父类定义了构造函数,那么子类的构造函数的第一句代码必须是super()的。
其他相关推荐构造方法_百度百科
声明:百科词条人人可编辑,词条创建和修改均免费,绝不存在官方及代理商付费代编,请勿上当受骗。
构造方法是一种特殊的方法,它是一个与类同名且没有返回值类型的方法。对象的创建就是通过构造方法来完成,其功能主要是完成对象的初始化。当类实例化一个对象时会自动调用构造方法。构造方法和其他方法一样也可以重载。
构造方法定义
在Java中,任何变量在被使用前都必须先设置初值.Java提供了为类的赋初值的专门方法。
构造方法特殊性
构造方法是一种特殊的成员方法,它的特殊性反映在如下几个方面:
1.构造方法作用:(1).构造出来一个类的实例 (2).对构造出来个一个类的实例(对象)初始化。
2.构造方法的名字必须与定义他的类名完全相同,没有返回类型,甚至连void也没有。
3.主要完成对象的初始化工作,构造方法的调用是在创建一个对象时使用new操作进行的。
4.类中必定有构造方法,若不写,系统自动添加无参构造方法。接口不允许被实例化,所以接口中没有构造方法。
5.不能被static、final、synchronized、abstract和native修饰。
6.构造方法在初始化对象时自动执行,一般不能显式地直接调用.当同一个类存在多个构造方法时,java编译系统会自动按照初始化时最后面括号的参数个数以及参数类型来自动一一对应。完成构造函数的调用。
7.构造方法分为两种:无参构造方法 有参构造方法
构造方法可以被。没有参数的构造方法称为,与一般的方法一样,构造方法可以进行任何活动,但是经常将他设计为进行各种初始化活动,比如初始化对象的属性。
8.构造代码块:  (1)作用:给对象进行初始化,对象一建立就执行,而且优先于构造函数执行  (2)构造代码块和构造函数的区别:  构造代码块是给所有不同对象的共性进行统一初始化,构造函数是给对应的对象进行初始化
9.子类继承父类中,  ***子类的实例化过程  ***构造方法不能被子类继承  ***子类创建对象时,会先去创建父类的对象。  默认是去调用父类的无参构造方法。  ***子类构造方法中,第一行默认是super()  ***为什么子类中第一行会默认有super()  因为他继承父类的成员使用,使用前这些成员必须初始化,  而他们是父类的成员,所以,必须通过父类进行初始化。  所以,会先创建一个父类的对象。  **当父类没有无参构造方法时
必须使用this或者super调用其他的构造方法。
10.自定义类中,如果不写构造方法,java系统会默认添加一个无参的构造方法。如果写了一个有参的构造方法,就一定要写无参构造方法。  如果想使用无参的构造方法,就必须手动给出无参构造方法。  建议:一般情况下,我们自定义的类都要手动给出无参构造方法。
具体使用:分别计算长、宽为20、10和6、3的两个长方形的面积。
class RectConstructor{
double area(){
return length*
RectConstructor(double width,double length){//带参数的构造方法
this.length=
this.width=
public class RectDemo{
public static void main(String args[]) {
RectConstructor rect1=new RectConstructor(10,20);
RectConstructor rect2=new RectConstructor(3,6);
ar=rect1.area();
System.out.println(&第一个长方形的面积是&+ar);
ar=rect2.area();
System.out.println(&第二个长方形的面积是&+ar);
控制台输出结果为:
第一个长方形的面积是200
第二个长方形的面积是18
构造方法软道语录
构造方法:
构造方法就是类构造对象时调用的方法,主要用来实例化对象。
java 核心技术博客分类:
学习java也有两年了,对一些基础还是理解的不够深,上网一搜很的确有不少这样的文章。下面就整理下以免以后忘记了。(理解:java 构造方法不等于创建对象而是初始化对象,new 关键字分配内存和创建对象)如理解有误的话,还请指点!
一、这个文章理解的还是比较有趣的。(转)
关于java的构造方法有几个简单的问题:
1.构造方法有返回值吗?
&&&& 没有。构造方法没有任何返回类型,也不允许是void。比如:
Java代码&
public class Test {
 //这不是构造函数!
public void Test() {
System.out.println("void Test()");
public static void main(String[] args) {
Test test = new Test();
test.Test();
&&&&& 这里我们定义了一个返回类型为void的Test()方法,有了返回类型void,它就不是构造方法了。
&&&& Test test = new Test();
&&&& 有人用上面的表达式来说明构造方法返回对象引用,这是明显错误的。new关键字有两个作用。一是分配内存,
创建对象。二是调用构造方法,完成对象的初始化工作。完成这两步之后,才算创建了一个完整的Java对象。我们
可以用Test test = new Test();的反编译代码来说明这个问题:
0:&&& new&&& #5; //class Test
3:&&& dup
4:&&& invokespecial&&& #6; //Method "":()V
7:&&& astore_1
&&&&& 原表达式被编译成四条bytecode指令。
new指令负责根据参数分配内存并创建Test对象,然后将新创建对象的引用置于栈顶。
dup指令复制栈顶的内容,记住,此时栈最顶端的两个单元都是新创建对象的引用。
接着是调用初始化方法,该方法是由构造方法编译而来,栈顶的引用作为此方法的参数消耗了。通过调用初始化方法完成
对象的创建过程。这里注意一下初始化方法Method "":()V,它是void类型的。
最后的astore_1指令将栈顶的对象引用赋给局部变量(前面说了,dup之后栈顶两个引用,一个给初始化方法吃掉了,一个留给astore_1操作用),也就是执行赋值操作。
&&&& 因此,得到的引用是new指令的结果,不是构造方法的返回值。
&&&& 有一点需要注意:new指令创建对象,同时使对象的各个字段得到其默认值,比如整数为0,浮点数为0.0,引用为null,boolean为false等。也就是说在构造方法执行之前,各个字段都有默认值了。这一点我们在第三条继续说明。
&&&& 通过上面说明,我们明确了构造方法的职能(初始化new指令创建的对象,得到一个状态合法的对象,完成对象的
创建过程)。任何类都有构造方法,但是new指令只能创建非抽象类的对象。理解了这一点,也就不要再问"抽象类也有构造方法,为什么不能创建对象"之类的问题了。
2.构造方法是静态的?
&&&&& 错误。
&&&&& 这是《Thinking In Java》中的一个观点。书里有一段:
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文件。
&&&& 这里我并没有看明白作者为什么说构造器实际上是静态方法。但是我们知道,静态方法是不能使用this的。因此,"构造器实际上也是静态方法"这点很好否定。看下面例子:
public class Test {
public Test() {
this.test2();
public static void test(){
this.test2();
public static void test2(){
&& test方法编译错误,因为静态方法中不能使用非静态的this,而构造方法使用this是没有问题的。
&&&&& 如果有C++经验的话,可以类比一下。C++里的new操作符有两个作用,调用operator new()来分配内存,然后调用构造函数来完成初始化。
&&&& 而这里的operator new()是隐式静态的。参考《C++程序设计语言(特别版)》中文版的
比如这个例子:
Cpp代码&
class Employee{
void* operator new(size_t);
void operator delete(void* ,size_t);
&&&& 成员operator new()和operator delete()默认为static成员,因为它们没有this指针,也不会修改任何对象。它们将提供一些存储,供构造函数去初始化,而后由析构函数去清理。
  类比可知,静态的是负责分配内存的工具,而不是构造函数。 不知道《Thinking In Java》的作者是不是把这点弄混了?
3.父类的构造方法中调用被子类重写的方法有多态现象。
&&&& 这句话很绕,直接看例子:
Java代码
class Father{
private int i = 5;
public Father() {
System.out.println("Father's i is " + this.i);
public void test(){
System.out.println(this.i);
class Son extends Father{
private int i = 55;
public Son() {
System.out.println("Son's i is " + this.i);
public void test() {
System.out.println(this.i);
public class Test {
public static void main(String[] args) {
new Son();
结果是:
Father's i is 5
0
Son's i is 55
&&&& 结合第一点,构造方法调用之前,首先是new指令创建了一个对象,并将各个成员初始化为其默认值。下面看构造方法的调用过程。
&&&& 子类构造方法会调用父类构造方法,父类构造方法首先打印Father's i is 5。然后调用test()方法,注意,我们创建的是Son类的对象,所以test()方法调用的是Son类定义的test()方法,也就是说发生了多态。我们再去看Son类中test方法的实现,就是简单的输出this.i,为什么是0呢,别忘了我们还没有执行子类的构造方法啊,所以此时子类的i还是new指令初始化得到的0。好,test()方法执行完了,总算回到子类构造方法继续执行,先把i赋值为55,下面的输出语句Son's i is 55也就不难理解了。
&&& 在构造方法中调用方法要特别注意这种多态现象。
&&& 这种现象和c++里的现象是不同的。在C++中,如果在父类的构造函数中调用虚方法的话,调用的是父类定义的版本,不会发生多态现象。但一个有趣的现象是,C++的经典书籍和Java的经典书籍竟然都建议不要在构造方法里面调用多态方法,因为现象并不是你期待的!这就奇怪了,难道C++程序员和Java程序员天生就有相反的期待吗?
浏览: 54699 次
来自: 福建
感谢,看了这么多这个是最明白的。
赞~楼主写的很清晰~
quartz调用任务xml配置文件
&?xml vers ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'posts - 14,&
comments - 7,&
trackbacks - 0
&&&&&&&&&&我相信大多说人都对构造方法、方法不陌生,而且很了解,但我也相信有很多像我这样的没有一个很好很清晰很全面的认识,今天就把它整理如下,希望能给大家带来点方便与帮助,也希望大家多多讨论。
&&&&&&&&&&构造方法和方法的区别:&&&&&&&&&
&&&& 构造方法要与类名相同,无返回类型,在类初始化的时候调用。&
&&&&&方法最好与类名不同,对象调用,静态方法可用类名.方法().
&&&& 构造器和方法在下面三个方面区别:修饰符,返回值,命名。
&&&&&& 1。和方法一样,构造器可以有任何访问的修饰: public, protected, private或者没有修饰(通常被package 和 friendly调用). 不同于方法的是,构造器不能有以下非访问性质的修饰: abstract, final, native, static, 或者 synchronized。
&&&&&&& 2。返回类型也是非常重要的。方法能返回任何类型的值或者无返回值(void),构造器没有返回值,也不需要void。
&&&&&&& 3。两者的命名。构造器使用和类相同的名字,而方法则不同。按照习惯,方法通常用小写字母开始,而构造器通常用大写字母开始。构造器通常是一个名词,因为它和类名相同;而方法通常更接近动词,因为它说明一个操作。
&&&&构造方法和方法中this和supper的用法区别:&
&& &"this"的用法&
&&& 构造器和方法使用关键字this有很大的区别。方法引用this指向正在执行方法的类的实例。静态方法不能使用this关键字,因为静态方法不属于类的实& 例,所以this也就没有什么东西去指向。构造器的this指向同一个类中,不同参数列表的另外一个构造器,我们看看下面的代码:&
package&com.dr.
public&class&Platypus&{
&&&&String&
&&&&Platypus(String&input)&{
&&&&&&&&name&=&
&&&&Platypus()&{
&&&&&&&&this("John/Mary&Doe");
&&&&public&static&void&main(String&args[])&{
&&&&&&&&Platypus&p1&=&new&Platypus("digger");
&&&&&&&&Platypus&p2&=&new&Platypus();
&&&&&&&&System.out.println(p1.name&+&"----"&+&p2.name);
在上面的代码中,有2个不同参数列表的构造器。第一个构造器,给类的成员name赋值,第二个构造器,调用第一个构造器,给成员变量name一个初始值 "John/Mary Doe".
在构造器中,如果要使用关键字this,那么,必须放在第一行,如果不这样,将导致一个编译错误。
在一个构造方法中只能调用一次其它的构造方法,并且调用构造方法的语句必须是第一条语句。
&& "super"的用法
&& 构造器和方法,都用关键字super指向超类,但是用的方法不一样。方法用这个关键字去执行被重载的超类中的方法。看下面的例子:
package&com.dr.
class&Mammal&{
&&&&void&getBirthInfo()&{
&&&&&&&&System.out.println("born&alive.");
class&Platypus1&extends&Mammal&{
&&&&void&getBirthInfo()&{
&&&&&&&&System.out.println("hatch&from&eggs");
&&&&&&&&System.out.print("a&mammal&normally&is&");
&&&&&&&&super.getBirthInfo();
在上面的例子中,使用super.getBirthInfo()去调用超类Mammal中被重载的方法。
构造器使用super去调用超类中的构造器。而且这行代码必须放在第一行,否则编译将出错。看下面的例子:
public&class&SuperClassDemo&{
&&&&SuperClassDemo()&{
class&Child&extends&SuperClassDemo&{
&&&&Child()&{
&&&&&&&&super();
在上面这个没有什么实际意义的例子中,构造器 Child()包含了 super,它的作用就是将超类中的构造器SuperClassDemo实例化,并加到 Child类中。
编译器自动加入代码 ,当我们写一个没有构造器的类,编译的时候,编译器会自动加上一个不带参数的构造器。
现在具体介绍一下构造方法的几种用法:
类的继承机制使得子类可以使用父类的功能(即代码),并且子类也具有父类的类型。下面介绍类在继承关系上的初始化的顺序问题。
class&SuperClass&
SuperClass()&
System.out.println("SuperClass&constructor");&
public&class&SubClass&extends&SuperClass&
SubClass()&
System.out.println("SubClass&constructor");&
public&static&void&main(String[]&args)&
SubClass&sub&=&new&SubClass();&
输出结果: SuperClass&constructor
&&&&&&&&&&& SubClass constructor
  在子类中只实例化了一个子类对象。从输出结果上看,程序并不是一开始就运行自己的构造方法,而是先运行其父类的默认构造方法。注意:程序自动调用其父类的默认构造方法。
class&SuperClass&
SuperClass(String&str)&
System.out.println("Super&with&a&string.");&
public&class&SubClass&extends&SuperClass&
SubClass(String&str)&
System.out.println("Sub&with&a&string.");&
public&static&void&main(String[]&args)&
SubClass&sub&=&new&SubClass("sub");&
在JDK下编译此程序不能成功。正如上例中说的:程序在初始化子类时先要寻找其父类的默认构造方法,结果没找到,那么编译自然不能通过。
  解决这个问题有两个办法:
  1.在父类中增加一个默认构造方法。
  2.在子类的构造方法中增加一条语句:super(str); 且必须在第一句。
  这两种方法都能使此程序通过编译,但就本程序来说运行结果却不相同。
  第1种方法的运行结果是:
  Sub with a string.
  第2种方法的运行结果是:
&&& Super with a string.&
&&&&Sub with a string.
  第2种解决方法实际上是指定编译器不要寻找父类的默认构造方法,而是去寻找带一个字符串为参数的构造方法。
  下面介绍对象的初始化顺序问题。
  示例3:
class&One&
One(String&str)&
System.out.println(str);&
class&Two&
One&one_1&=&new&One("one-1");&
One&one_2&=&new&One("one-2");&
One&one_3&=&new&One("one-3");&
Two(String&str)&
System.out.println(str);&
public&class&Test&
public&static&void&main(String[]&args)&
System.out.println("Test&main()&start");&
Two&two&=&new&Two("two");&
输出结果:
Test main() start...
  在main()方法中实例化了一个Two类的对象。但程序在初始化Two类的对象时,并非先调用Two类的构造方法,而是先初始化Two类的成员变量。这里Two类有3个成员变量,它们都是One类的对象,所以要先调用3次One类的相应的构造方法。最后在初始化Two类的对象。
&即在创建对象时,对象所在类的所有数据成员会首先进行初始化,如果其中的成员变量有对象,那么它们也会按照顺序执行初始化工作。在所有类成员初始化完成后,才调用对象所在类的构造方法创建对象。构造方法作用就是初始化。
  示例4:
class&One&
One(String&str)&
System.out.println(str);&
class&Two&
One&one_1&=&new&One("one-1");&
One&one_2&=&new&One("one-2");&
static&One&one_3&=&new&One("one-3");&
Two(String&str)&
System.out.println(str);&
public&class&Test&
public&static&void&main(String[]&args)&
System.out.println("Test&main()&start");&
Two&two_1&=&new&Two("two-1");&
System.out.println("------------");&
Two&two_2&=&new&Two("two-2");&
输出结果:
Test main() start...
------------
  如果一个类中有静态对象,那么它会在非静态对象前初始化,但只初始化一次。非静态对象每次调用时都要初始化。
class&One&
One(String&str)&
System.out.println(str);&
class&Two&
One&one_1&=&new&One("one-1");&
One&one_2&=&new&One("one-2");&
static&One&one_3&=&new&One("one-3");&
Two(String&str)&
System.out.println(str);&
<span style="color: #&
public&class&Test&
static&Two&two_3&=&new&Two("two-3");&
public&static&void&main(String[]&args)&
System.out.println("Test&main()&start");&
Two&two_1&=&new&Two("two-1");&
System.out.println("------------");&
Two&two_2&=&new&Two("two-2");&
输出结果:
Test main() start...
------------
  程序中主类的静态变量会在main()方法执行前初始化。结果中只输出了一次one-3,这也说明:如果一个类中有静态对象,那么它会在非静态对象前初始化,但只初始化一次。非静态对象每次调用时都要初始化。
class&One&
One(String&str)&
System.out.println(str);&
class&Two&
static&int&i&=&<span style="color: #;&
One&one_1&=&new&One("one-1");&
static&One&one_2&=&new&One("one-2");&
static&One&one_3&=&new&One("one-3");&
Two(String&str)&
System.out.println(str);&
public&class&Test&
public&static&void&main(String[]&args)&
System.out.println("Test&main()&start");&
System.out.println("Two.i&=&"&&Two.i);&
输出结果:
Test main() start...
  不仅第1次创建对象时,类中所有的静态变量要初始化,第1次访问类中的静态变量(没有创建对象)时,该类中所有的静态变量也要按照它们在类中排列的顺序初始化。
初始化的顺序包括构造方法调用的顺序如下:
  1.主类的静态成员首先初始化。
  2.主类的超类的构造方法按照从最高到最低的顺序被调用。
  3.主类的非静态对象(变量)初始化。
  4.调用主类的构造方法。
阅读(37706)
&re: java中构造方法和方法全面解析
为什么非要调用父类的构造方法&&&&&&
2010年11月
313456789111314151617181920212223242526272829301234567891011
留言簿(13)
阅读排行榜
评论排行榜

我要回帖

更多关于 无参构造方法的作用 的文章

 

随机推荐