0

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");

6 Answers 6

2

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Isn't there other option?
0

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;
    }
}

Comments

0

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!

Comments

0

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

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.