Ninja
Ninja

Reputation: 357

java - can't create a constructor

class Human{

    // declared instance variables 
    String name;
    int age;

    // instance method
    void speak(){
        System.out.println("My name is: " + name);
    }

    int calculateYearsToRetirement(){
        int yearsLeft = 65 - age;
        return yearsLeft;
    }

    int getAge(){
        return age;
    }

    String getName(){
        return name;
    }

    // so when I create an instance, i can't have constructor?
    // error here
    Human(int age){
        age = this.age;
    }
}



}
public class GettersAndReturnValue {

    public static void main(String[] args) {
        // error here because I created a constructor Human(int a)
        Human human1 = new Human();
        human1.name = "Joe";
        human1.age = 25;

        human1.speak();
        int years = human1.calculateYearsToRetirement();

        System.out.println("Years till retirements " + years);

        int age = human1.getAge();
        System.out.println(age);
    }

}

I tried to create a constructor Human(int age) to practice 'this' keyword and to change the age from 25 to something else but I get an error because I have one Human class and one Human constructor. When I try to create an instance of Human Type in my main method, eclipse is asking me to remove the constructor

Upvotes: 0

Views: 3544

Answers (5)

Yuva Raj
Yuva Raj

Reputation: 3881

Here's the working code :

Create a class GettersAndReturnValue and add this. You need a empty constructor.

class Human{

    // declared instance variables 
    String name;
    int age;

    // instance method
    void speak(){
        System.out.println("My name is: " + name);
    }

    int calculateYearsToRetirement(){
        int yearsLeft = 65 - age;
        return yearsLeft;
    }

    int getAge(){
        return age;
    }

    String getName(){
        return name;
    }

    // so when I create an instance, i can't have constructor?
    // error here
    Human(int age){
        this.age = age;
    }

    public Human() {
        // TODO Auto-generated constructor stub
    }
}



public class GettersAndReturnValue {

    public static void main(String[] args) {
        // error here because I created a constructor Human(int a)
        Human human1 = new Human();
        human1.name = "Joe";
        human1.age = 25;

        human1.speak();
        int years = human1.calculateYearsToRetirement();

        System.out.println("Years till retirements " + years);

        int age = human1.getAge();
        System.out.println(age);
    }

}

Output :

My name is: Joe
Years till retirements 40
25

Upvotes: 0

Raphael Amoedo
Raphael Amoedo

Reputation: 4465

When you create a non-empty constructor, the empty constructor will not be available anymore. You do can have more than one constructor, but if you want the no-argument constructor along with other, you will have to recreate it.

//Please, make it public for constructors
public Human(int age){
   this.age = age; //this.age first, to receive the parameter age
}

public Human() {} //Empty constructor. It doesn't has to be a content.

So you call:

Human humanOne = new Human(); //Using no-argument constructor
Human humanTwo = new Human(25); //Using constructor with int to set age

Upvotes: 1

Buddhika
Buddhika

Reputation: 13

When you create a constructor in the class, it will no longer use the default constructor. In your code, you've created a public Human(int) constructor, so there is no default constructor. Because of that, you cannot create human object like this:

Human a = new Human();

To do that, you have to manually implement a no-argument Human constructor.

Here is a solution:

class Human{

    String name;
    int age;

    //default constructor
    public Human (){
    }


    //paramete constructor
    public Human(int a){
        this.age=a;
    }

    void speak(){
        System.out.println("My name is: " + this.name);
    }

    int calculateYearsToRetirement(){
        int yearsLeft = 65 - age;
        return yearsLeft;
    }

    int getAge(){
        return this.age;
    }

    String getName(){
        return this.name;
    }
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

You've swapped the order in your assignment,

Human(int age){
    age = this.age;
}

should be something like (don't forget to initialize name too)

Human(int age){
    this.age = age;
    this.name = "Unknown";
}

You're assigning the default value 0 to the passed in parameter. If you provide a constructor then the compiler will no longer insert the default constructor,

Human() {
    this.age = 0;
    this.name = "Unknown";
}

and you might as well add a constructor that takes the name,

Human(int age, String name) {
    this.age = age;
    this.name = name;
}

then you could call it (in main) like

Human human1 = new Human(25, "Joe");
// human1.name = "Joe";
// human1.age = 25;

Upvotes: 2

Anko6
Anko6

Reputation: 41

You have to create a no parameter constructor, because when you are calling Human h = new Human();, you are calling a no parameter constructor.

Try doing this instead:

Human h = new Human(age);

Upvotes: 1

Related Questions