java - how to create instance of a object without passing argument in constructor? -
i have student class , has constructor student(int id, string name)
now, want make object of student class without touching constructor. how can it?
like student std = new student();
not student std = new student(1, "benjamin");
create default constructor, like
student();
in java, default constructor refers nullary constructor automatically generated compiler if no constructors have been defined class. default constructor implicitly calls superclass's nullary constructor, executes empty body. also, can write own self.
note that, there can many constructors according coding design , requirement. say,
class student { // default constructor public student() {} // 1 param constructor public student(int id) { this.id = id; } // 2 param constructor public student(int id, string name) { this.id = id; this.name = name; } }
if have default constructor, without
student std = new student(1, "benjamin");
you can create std
object like:
student std = new student();
Comments
Post a Comment