Using "this" with class name

JavaAndroidThis

Java Problem Overview


I am doing Android programming and was learning about Intents, when I saw a constructor that, to my C# trained mind, seemed funky. The call was:

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);

Both of the parameters are new to me. How is there a static ".this" off of a Class Name? Is this a Java thing or an Android thing? I am assuming that it is the same as just saying "this", since I am in the context of CurrentActivity, but I don't get how the "this" can be called off of the Class name itself. Also. The ".class" looks like it is used for reflection, which I am familiar with in C#, but any insight into this would be welcomed as well.

Thanks.

Java Solutions


Solution 1 - Java

Usually, you can use only this. But, sometimes this makes reference to an inner class... so, for example:

Button button = (Button)findViewById(R.id.ticket_details_sell_ticket);
button.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View v) {
		// it will be wrong to use only "this", because it would
		// reference the just created OnClickListener object
		Intent login = new Intent(ClassName.this, Login.class);
		startActivityForResult(login, LOGIN_REQUEST);
	}
});

Solution 2 - Java

One at a time:

The first construct is called a qualified this. The purpose of the syntax is in the case where you are in an inner class (typically an anonymous inner class) and you want to reference the this of the outer class rather than the this of the (anonymous) inner class. The "qualified this" can only be used in a context where this would be ambiguous. The quote the JLS "It is a compile-time error if the expression occurs in a class or interface which is not an inner class of class T or T itself".

The second construct is called a class literal is the way to reference the Class object that represents that type. It can be used in any context.

Solution 3 - Java

The syntax "Classname.this" is for inner classes. If you want to refer to the enclosing instance of type "Outerclass" then you do it as "Outerclass.this".

NextActivity.class is simply the Class object that describes class "NextActivity".

Solution 4 - Java

NextActivity.class in java means typeof(NextActivity) in C#

Solution 5 - Java

ClassName.this is used to reference the current instance of an outerclass from an inner class.

Solution 6 - Java

<ClassName>.this

is used in nested classes to refer to the current instance of the enclosing class, since the `this' keyword refers to the nest class instance.

public class Siht {
class NestedSiht {
void demoThis() {
System.err.println("public class Siht {
class NestedSiht {
void demoThis() {
System.err.println("this' is an instance of: " +  							this.getClass().getName()); 			System.err.println("Siht.this' is an instance of: " +
Siht.this.getClass().getName());
}
}



void demoThis() {
	new java.lang.Object() {
		void demoThis() {
			System.err.println("`this' is an instance of: " + 
							this.getClass().getName());
			System.err.println("`Siht.this' is an instance of: " +
							Siht.this.getClass().getName());
		}
	}.demoThis();
	new NestedSiht().demoThis();
}

public static void main(String [] args) {
	new Siht().demoThis();
}




}


Siht.this' is an instance of: " +
Siht.this.getClass().getName());
}
}

void demoThis() {
	new java.lang.Object() {
		void demoThis() {
			System.err.println("`this' is an instance of: " + 
							this.getClass().getName());
			System.err.println("`Siht.this' is an instance of: " +
							Siht.this.getClass().getName());
		}
	}.demoThis();
	new NestedSiht().demoThis();
}

public static void main(String [] args) {
	new Siht().demoThis();
}

}

Solution 7 - Java

It's confusing only because when you use "MainActivity.this", it seems that you are referring to the class and not the object. In reality when you use "this" you are always referring to the current object, as the java se documentation states:

https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

> Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

It's just syntactically peculiar.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskazView Question on Stackoverflow
Solution 1 - JavaCristianView Answer on Stackoverflow
Solution 2 - JavaYishaiView Answer on Stackoverflow
Solution 3 - JavaDJClayworthView Answer on Stackoverflow
Solution 4 - JavacodymanixView Answer on Stackoverflow
Solution 5 - JavaakfView Answer on Stackoverflow
Solution 6 - JavakhachikView Answer on Stackoverflow
Solution 7 - JavaGaetano PiazzollaView Answer on Stackoverflow