user3127109
user3127109

Reputation: 3763

how to create instance of a object without passing argument in constructor?

I have a Student class and it has constructor Student(int id, string name)

Now, I want to make object of Student Class without touching the constructor. How can I do it?

Like Student std = new Student();

Not Student std = new Student(1, "Benjamin");

Upvotes: 0

Views: 12369

Answers (6)

Vee
Vee

Reputation: 21

I have same issue though the thing is that default constructor that should be automatically implemented should allow object without arguments. Error states: Cannot resolve constructor. :(

User user = new User();
user.setUsername(registerRequest.getUsername());
user.setPassword(registerRequest.getPassword());
user.setEmail(registerRequest.getEmail());

So User(); demands arguments and I cannot apply all other lines of code with user

Upvotes: 0

mazhar islam
mazhar islam

Reputation: 5619

Create a default constructor, like

Student();

In Java, a default constructor refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor implicitly calls the superclass's nullary constructor, then executes an empty body. Also, you can write it by your own self.

Note that, there can be many constructors according to your coding design and requirement. Say,

class Student {  

    // default constructor
    public Student() {}

    // one param constructor
    public Student(int id) {
        this.id = id;
    }

    // two param constructor
    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

If you have the default constructor, then without

Student std = new Student(1, "Benjamin");

you can create a std object like:

Student std = new Student();

Upvotes: 2

Rajesh
Rajesh

Reputation: 2155

You can do something like this; provided your class has default empty constructor and setter methods :

Student st = new Student();
st.setId(1);
.
..

Upvotes: 0

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

You have to add a constructor without parameters.

Note this is a solution only for academic purpose: If you don'like to create a new constructor without arguments there is another possibility. Delete the constructor with parameters, if you do that automatically a default constructor without parameters is added to your class. Obviously if you do that you have to change existing code replacing old calls to the parameterized constructor with the new one without parameters!

Upvotes: 0

Eran
Eran

Reputation: 393831

You must create a parameterless constructor - Student(). If you didn't have the Student(int id, string name) constructor, the compiler would have created an empty parameterless constructor automatically.

Upvotes: 1

Tagir Valeev
Tagir Valeev

Reputation: 100209

You should create a second constructor which has no arguments (so-called "default constructor") in your class:

class Student {
    int id;
    String name;

    // new constructor
    public Student() {
    }

    // old constructor: we don't change it.
    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

Upvotes: 0

Related Questions