0
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

4
  • First define a constructor public Human ( int a ), along with no-args constructor ( if need be ) inside the Human class, only then you can use that inside the main method of GettersAndReturnValue class, while creating an object of the class. Commented May 10, 2015 at 4:33
  • "eclipse is asking me to remove the constructor" And what does it tell you? The compiler messages are not for nothing. Commented May 10, 2015 at 4:35
  • Also "I get an error because I have one Human class and one Human constructor", why do you think this is the reason? Commented May 10, 2015 at 4:39
  • Thank you everyone for your help! In my main method, I wrote Human(5); // I thought this would call the Human constructor but I wasn't // using the human object I created.. I should've put // Human Human1 = new Human(5); Commented May 11, 2015 at 6:23

5 Answers 5

2

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

8 Comments

Ahha, I missed that the constructor is already created. I thought, the OP never did created one :-)
The constructor should not be public?
@akhil_mittal A constructor might be public, private, protected or (as in this case) default (a.k.a package level).
@akhil_mittal It doesn't have to. The visibility modifier depends on what you want to expose it to.
@ElliottFrisch The swap is a good catch, but what about the lack of an empty constructor in response to the OP's question?
|
1

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

3 Comments

The OP wants to create a Human from an empty constructor, why do you tell him to do it differently?
@user1803551: What makes you so confident that this is the case? From what I'm looking at, it's much more likely the case that the OP hadn't realized that their new constructor overtook the default, no-arg constructor. I'd attribute the fact that they're saying new Human() as a mistake and not as intentional.
@Makoto Nothing. In fact, in a way I was asking the answerer why they think it's not the case.
1

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 

2 Comments

Terminology nitpick: if you create a constructor yourself, it's not a "default" constructor. "Default" by definition is something the compiler supplies by default when you don't provide one. I usually call this a no-argument constructor.
You can rephrase: "When you create a non-empty constructor, the empty constructor will not be available anymore.".
0

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

Comments

0

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 

1 Comment

Don't forget to fix age = this.age.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.