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

This Keyword in Constructors

I never used "this" for constructors as I cannot set it to a global scope, but just for curiosity sake, how do you do it properly? I did it like this, and it says cannot find symbol for "this":

public class HelloWorld{

     public static void main(String []args){

        myClass a = new myClass("Jamie", 3);
     }
}

class myClass{
    myClass(String name, int number) {
        this.name = name;
        this.number = number;
    }
}

ideas why it's wrong?

1 Answer

To set a variable to have a global scope it needs to be declared as public. Your "name" and "number" vars only have scope from within your constructor, so you would need to declare variables outside of your constructor in order to have your code compile and give your vars a global scope. Here's a quick example:

HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        MyClass mc = new MyClass("Jamie", 3);
        System.out.println(mc.mName);
        System.out.println(mc.mNumber);
        /*System.out.println(mc.name);  this would cause a compiler error*/
        /*System.out.println(mc.number);  this would cause a compiler error*/
    }
}

class MyClass {
    public String mName;
    public int mNumber;
    MyClass(String name, int number) {
        this.mName = name;
        this.mNumber = number;
        System.out.println(mName);
        System.out.println(mNumber);
        System.out.println(name);
        System.out.println(number);
    }
}

OUTPUT

Jamie
3
Jamie
3
Jamie
3

In this case, the only vars that have global scope are the mName and mNumber variables. the name and number vars only have scope from within your constructor. Also it's worth noting that I changed the name of "myClass" to "MyClass", with a capital "M", to follow java naming convention.

Ah cool! instead of naming a different name to "name", could you do:

class myClass{
    myClass(String name, int number) {
        this.name = name;
        this.number = number;
        System.out.println(name);
    }
}

or is that a syntax error? the compiler tells me it is, but I'm not sure if I'm writing properly.

The variable name is irrelevant, if you read my above post I go into why your compiler is spitting an error out at you. To keep the same names "name" and "number" you would just need to change the name of your variables that you declared outside of the constructor.

class MyClass {
    public String name;
    public int number;
    MyClass(String name, int number) {
        this.name = name;
        this.number = number;
        System.out.println(name);
        System.out.println(number);
    }
}

So sorry! was super tired yesterday, not sure what my brain was thinking! Thank you for everything!

No problem, let me know if you have any other questions.