What is the difference between Class.this and this in Java
In this case, they are the same. The Class.this
syntax is useful when you have a non-static nested class that needs to refer to its outer class's instance.
class Person{
String name;
public void setName(String name){
this.name = name;
}
class Displayer {
String getPersonName() {
return Person.this.name;
}
}
}
What are the differences between this and this() in java
The first example calls the overloaded constructor in the default constructor. You can call all overloaded constructors this way. It has to be the first line in the constructor, just like calls to super().
The second one shows how the special name this
refers to the current instance within the class. It's only required to sort out name duplication:
public class ThisDemo {
private static final String DEFAULT_VALUE = "REQUIRED";
private String value;
public ThisDemo() {
this(DEFAULT_VALUE);
}
publi ThisDemo(String value) {
// Required here because the private member and parameter have same name
this.value = value;
}
public String getValue() {
// Not required here, but I prefer to add it.
return value;
}
}
Difference between this and .this?
The Class.this
syntax is useful when you have a non-static nested class that needs to refer to its enclosing class's instance.It is only used in cases where there is an inner class, and one needs to refer to the enclosing class
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
.
A good example
public class TestForThis {
String name;
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
class TestForDotThis {
String name ="in";
String getName() {
return TestForThis.this.name;
}
}
public static void main(String[] args) {
TestForThis t = new TestForThis();
t.setName("out");
System.out.println(t.getName());
TestForThis.TestForDotThis t1 = t.new TestForDotThis();
System.out.println(t1.getName());
}
}
Output will be
out
out
What's the diffrence between this::myMethod and ClassName::myMethod?
this::myMethod
refers to myMethod
on a specific instance of ClassName
- the instance that you put this::myMethod
in its code.
ClassName::myMethod
can refer to either a static method or an instance method. If it refers to an instance method, it may be executed on a different instance of ClassName
each time it is called.
For example:
List<ClassName> list = ...
list.stream().map(ClassName::myMethod)...
will execute myMethod
each time for a different ClassName
member of the list.
Here's a mode detailed example that shows the difference between these two type of method reference:
public class Test ()
{
String myMethod () {
return hashCode() + " ";
}
String myMethod (Test other) {
return hashCode() + " ";
}
public void test () {
List<Test> list = new ArrayList<>();
list.add (new Test());
list.add (new Test());
System.out.println (this.hashCode ());
// this will execute myMethod () on each member of the Stream
list.stream ().map (Test::myMethod).forEach (System.out::print);
System.out.println (" ");
// this will execute myMethod (Test other) on the same instance (this) of the class
// note that I had to overload myMethod, since `map` must apply myMethod
// to each element of the Stream, and since this::myMethod means it
// will always be executed on the same instance of Test, we must pass
// the element of the Stream as an argument
list.stream ().map (this::myMethod).forEach (System.out::print);
}
public static void main (java.lang.String[] args) {
new Test ().test ();
}
}
Output:
2003749087 // the hash code of the Test instance on which we called test()
1747585824 1023892928 // the hash codes of the members of the List
2003749087 2003749087 // the hash code of the Test instance on which we called test()
In Java, what is the difference between this.method() and method()?
The only time it matters is if you are using OuterClass.this.method()
e.g.
class OuterClass {
void method() { }
class InnerClass {
void method() {
OuterClass.this.method(); // not the same as method().
}
}
}
What's the difference between this and Activity.this
this
refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to.
Activity.this
points to the instance of the Activity you are currently in.
Related Topics
Why Java.Util.Optional Is Not Serializable, How to Serialize the Object with Such Fields
Combine Multiple Collections into a Single Logical Collection
Replace All Occurrences of a String Using Stringbuilder
In Java, When Does an Object Become Unreachable
How to Get the Threadpoolexecutor to Increase Threads to Max Before Queueing
How to Create a Topic in Kafka from the Ide Using API
I Found JPA, or Alike, Don't Encourage Dao Pattern
Java.Io.Ioexception: Mark/Reset Not Supported
How to Manage Exceptions Thrown in Filters in Spring
How to Use Jsch for Ssh Key-Based Communication
Why F Is Placed After Float Values
Sending Mail Attachment Using Java
JSONobject:Why JSONobject Changing the Order of Attributes
Rotating Image with Affinetransform