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 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.