Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

important question

why constructor is same name in our field?

i'm so confused about this subject why we put it on constructor argument same name in our field ?????? please help me

2 Answers

Oh, the parameter name doesn't matter. If you have it the same name, you have to put

this.name = name; 

But otherwise you could change it to anything you wanted. For instance:

class ABC{

     String name;

     public ABC(String cheese){
          name = cheese; 
     }

}

ow thanks

If you're asking why a constructor has the same name as a class, it's because it defines how you create an instance of the object in a different class.

no that's not my question let me show you an example :

class ABC{

string name;

public ABC(string name) my question is here why we put argument as same name like our field? { }

}