Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Creating the MVP Getting Started

Why using this.answer when we can just use manswer or any else name to clear define variables

i just mean to ask why we need to use this.answer in places like while creating hangman game in game.java while different variables can be clearly stated

1 Answer

Hey Priyanka,

Thats a good question. In the world of programming there are lots of rules you must follow for example the syntax of a language.(which is things we must do or the programming will throw an error) Then there are conventions, conventions are ideas or process that are agreed by the community so we can look at each others code and understand whats going on.

Lets look at an example;

public class Person{
     private String name;

     public Person(String name){
       // this - refers to this class 
      this.name = name;
       // but the following means the same
       name = name;
     }
}

Maybe we can say that by using "this" it makes the code more readable or could it be even more robust?

Note that i have declared the name variable buy just using name and not private String mName;

prefixing the letter m is just another naming convention, the m stands for member variable and has no impact on the code. Android uses the m prefix naming convention.

This maybe doesn't directly answer your question, but hopefully it has pointed your in the right direction. Maybe a good idea to look up naming conventions vs syntax in java.