r/javahelp • • Sep 12 '19

What is the difference between "[ClassName].this.variable" and simply, "this.variable"

So I came across this class called:

AutoCompleteComboBoxListener()

with a node called comboBox.

And inside its methods, such as

selectClosestResultBasedOnTextFieldValue(),

they refer to the comboBox of AutoCompleteComboBoxListener() as

AutoCompleteComboBoxListener.this.comboBox.getItems();

Is it different from:

this.comboBox.getItems();

Thanks a lot everyone.

17 Upvotes

9 comments sorted by

2

u/HBK05 Sep 12 '19

This refers to the current class, if you are already working in that class, there's no real point to use it besides coding conventions, some people swear you should use it for cleanness and easy/understandable code, and some don't care.

The keyword super can be used to refer to a variable that is inherited into the current class, but does not actually reside in said class. It's basically the other end to that, in-case you have the same variable name in an inherited class and the local class (for some reason..) you save yourself from that confusion.

0

u/[deleted] Sep 12 '19

[deleted]

8

u/8igg7e5 Sep 12 '19

And it's worth noting that, if you declare a non-static inner class, every instance has the overhead of a link to the outer instance whether you actually use it or not - and if you let references to instances of the inner class escape, they prevent GC of the outer instance.

Use static inner classes unless you really need that outer class relationship.

1

u/dg_713 Sep 16 '19

Thanks for the additional info. Can you explain further what you mean exactly when you said this?

1

u/8igg7e5 Sep 16 '19

Consider the following

class Outer {
    private int[] pool = ... ; // Let's say this is 10 million entries
    private Random rand = new Random();

    public Inner makeRandomValue() {
         return new Inner();
    }

    public class Inner {
        private int value;

        private Inner() {
            int i = Outer.this.rand.nextInt(Outer.this.pool.length);
            this.value = Outer.this.pool[i];
        }

        public int getValue() { return value; }
    }
}

If you do...

Outer outer = new Outer();
Inner inner = outer.makeRandomValue();

Then the outer instance will not be garbage collected until the inner instance is garbage collected.

Rearranged as follows, this is no longer a problem...

class Outer {
    private int[] pool = ... ; // Let's say this is 10 million entries
    private Random rand = new Random();

    public Inner makeRandomValue() {
         int i = rand.nextInt(pool.length);
         return new Inner(pool[i]);
    }

    public static class Inner {
        private int value;

        private Inner(int value) {
            this.value = value;
        }

          public int getValue() { return value; }
    }
}

Or something like that.

1

u/dg_713 Sep 16 '19

Ok, I get it now, it's because inner instance is still referencing the values stored in the outer instance, is that it?

1

u/8igg7e5 Sep 16 '19

No it's referencing the outer instance. An instance of a non-static inner class keeps a link to the instance of the outer class that was used to create it.

1

u/dg_713 Sep 16 '19

Ok. Thank you for this answer.

0

u/[deleted] Sep 12 '19

[deleted]

0

u/ZukoBestGirl Sep 12 '19

Not really. A non static inner class will always have access to it's parent. [ParentName].this.field.