欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java Constructor,

来源: javaer 分享于  点击 46629 次 点评:243

Java Constructor,


1.Constructor was introduced in C++,Java adopted it  from C++.

2.A constructor is a special method automatically called when an object is created.(also means to initialize the object).

3.The name of the constructor is exactly the same as the name of the Class.(Even the Upper case of the first letter).

4.A constructor takes NO arguments is called the default constructor.the constructor can also HAVE arguments to allow you tospecify how an object is created.

5.the constructor have no return value,this is distinctly different from a void return value.(the void can still have a option to return something else).


//ConstructorSample......

//Rock.java


class Rock {

Rock() {

System.out.println("This is the constructor of the Class Rock !");

}


Rock(int i) {

System.out.println("This is the constructor of the Class Rock using the argument "

+ i + "!");

}


public static void main(String[] args) {

Rock r1 = new Rock();

Rock r2 = new Rock(1);

}

}

相关文章

    暂无相关文章
相关栏目:

用户点评