Is it possible to use 'this' in the declaration of a final instance variable?

Is it possible to use the this in declaring / initializing a final instance variable in Java?

Like this:

 private final SomeClass foo = new SomeClass(this); 

It worked when I tried. And since this is not a static variable, I assume that it should refer to a specific instance. But I felt insecure if this was appropriate or not, so I wanted to ask here.

Edit: The main class is the Android activity class, and the SomeClass instance requires this operation as a context.

0
java android final
source share
4 answers

"This is technically fair." Indeed, this refers to a specific instance, namely the instance that contains the SomeClass instance.

But I would not recommend doing this in general. The exact behavior and state of this passed to the constructor depends on the subtle details. Consider the following example:

 class SomeClass { public SomeClass(DangerousSelfReference dangerousSelfReference) { System.out.println("State: "); System.out.println(" a: "+dangerousSelfReference.getA()); System.out.println(" b: "+dangerousSelfReference.getB()); System.out.println(" c: "+dangerousSelfReference.getC()); System.out.println(" d: "+dangerousSelfReference.getD()); System.out.println(" ref: "+dangerousSelfReference.getRef()); } } public class DangerousSelfReference { public static void main(String[] args) { DangerousSelfReference d = new DangerousSelfReference(); } private String a; private String b = "b"; private final SomeClass ref = new SomeClass(this); private final String c = "c"; private String d = "d"; DangerousSelfReference() { a = "a"; } String getA() { return a; } String getB() { return b; } String getC() { return c; } String getD() { return d; } SomeClass getRef() { return ref; } } 

I suppose this might make a tidy question about a job interview because it’s difficult to predict a way out. Amazing he prints

 State: a: null b: b c: c d: null ref: null 

Note that the final c variable is initialized, but not the final variable d has not yet been specified. In contrast, the non-final variable b (declared before the SomeClass instance) is already initialized.

Such subelements are always doubtful and should be avoided if possible.

+3
source share
 private final SomeClass foo = new SomeClass(this); private int bar = 42; 

The SomeClass constructor will find bar with 0.

So this is not so good.

+2
source share

My first problem: why do you need this?

As a rule, I would not recommend this, since it could potentially be dangerous in more complex scenarios, where the construction of SomeClass depends on the specific state of the past this object.

Consider, for example:

 class SomeClass { private Foo foo; SomeClass(Foo foo) { this.foo = foo; // do something based on state of foo // such as call int len = foo.myString.length(); // <- this will throw NPE, because // foo.myString is still null as Foo() constructor wasn't called yet } } 

and then your Foo class:

 class Foo { String myString = null; Foo() {/*constructor 1, perhaps calling init()*/ init(); } Foo(...params) {/*constructor 2*/} private void init() { // some initialization myString = "test String"; } // Note: this constructor is called before any of Foo // constructos are invoked // thus passed Foo "this" object is not initialized yet // (contains defaults for all fields) private final SomeClass someClass = new SomeClass(this); } 
+1
source share

Yes, it is normal.

since this has nothing to do with the final keyword of the Instance variable. what you do is passing the object of the enclosing class to the constructor of SomeClass

-one
source share

All Articles