Frage

I'm trying to fully understand how does "this" work. In my previous post I understood why we use the "this" keyword.

My understanding of static is that the class has one copy of that member. "this" is used to represent the current object. For all objects, the static member variable is same, then why does "this" works on static member variables?

code:

public class OuterInnerClassExample
{
    public static void main(String[] args)
    {
        OuterClass outClassObj = new OuterClass(10);
        outClassObj.setInt(11);
        outClassObj.setInt(12);

        System.out.println("Total Count: " + outClassObj.getCount() );
    }
}

class OuterClass
{
    private int test;
    private static int count;       // total sum of how many new objects are created & the times all of them have been changed

    public OuterClass(int test)
    {
        this.test = test;
//      count += 1;                 // preferred as count is static
        this.count += 1;            // why does this work
    }

    public int getInt()
    {
        return this.test;
    }

    public int getCount()
    {
        return this.count;
    }

    public void setInt(int test)
    {
        this.test = test;
//      count += 1;                 // preferred as count is static
        this.count += 1;            // why does this work
    }

    class SomeClass
    {
        private OuterClass outClassObj;

        public SomeClass(int var)
        {
            outClassObj = new OuterClass(var);
        }

        public int getVar()
        {
            return this.outClassObj.getInt();
        }

        public int getCount()
        {
            return this.outClassObj.getCount();
        }

        public void setVar(int var)
        {
//          OuterClass.this.test = var;         // can do this
            outClassObj.setInt(var);            // preferred

//          OuterClass.count += var;            // should do this
            OuterClass.this.count += var;       // why does this work
        }
    }
}

Also, in the setVar method, we can use ClassName.this to manipulate the objects value, but I think it is better to use a setter as it is much clearer. Is there any advantage of using "this" here that I am missing out?

Please post code to show examples of what you are trying to explain.

War es hilfreich?

Lösung

The underlying reason is that you can call a static member/method either on the class name MyClass.someStaticField or on an instance of the class new MyClass().someStaticField.

So as long as this is "available", MyClass.someStaticField is equivalent to this.someStaticField.

In your case, you always call this from instance methods so it does exist and the statement compile.

And finally, inner classes (non-static nested classes) contain a reference to their enclosing class, which allows OuterClass.this.count to compile too.

Andere Tipps

Whenever you do (some expression).staticMember, the compiler only uses the compile-time type of the expression to determine what static member to access. It doesn't matter what the expression is or what value it evaluates to.

It could be ((MyClass)null).someStaticField and it would not make a difference.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top