What does this.object.method(this) do? [duplicate] - java

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 3 years ago.
For example object1 has an instance method1:
public void method1() {
this.object2.method2(this);
}
Would the first 'this' refer to the object 1 and the second 'this' object 2 ?

In your code the this keyword give the method a sense of which variable, object or instance it will call, while this can also call the parents variable, object, instance or method. So when it calls this.object2.method2(this) it actually calling for the object2's method2 with a parameter of the class.
In your scope there is a code that instantiate the object2 and it is declared outside any method of the class. See more in here: use of this (javadocs)

Related

Java Methods vs constructors parameter comparison [duplicate]

This question already has answers here:
Methods vs Constructors in Java
(11 answers)
Closed 2 years ago.
actually i'm beginner my questions might be silly And my doubt is what was actual purpose of methods vs constructors in java,
we can pass values in both methods & constructor parameters,
which one is recommended and why??
A Java method is use to perform some action which is also know as Function. Where you can pass parameter. They must have return type.
A Constructor is special method use to initialise the object . Constructor must not have return type. Constuctor name must be same as class name.
Constructor is used to initialize an object whereas method is used to exhibits functionality of an object.
Constructors are invoked implicitly whereas methods are invoked explicitly.
Constructor does not return any value where the method may/may not return a value.
In case constructor is not present, a default constructor is provided by java compiler. In the case of a method, no default method is provided.
Constructor should be of the same name as that of class. Method name should not be of the same name as that of class.

NullPointerException when calling a method reference to an arbitrary object with null argument [duplicate]

This question already has answers here:
Java 8 method reference to class instance method NPE [duplicate]
(2 answers)
Closed 3 years ago.
I was trying to create a method reference to an arbitrary object, so I defined the following types:
interface I {
boolean get(Impl impl);
}
static class Impl {
public boolean get() {
return true;
}
}
Then I declared the method reference, like below:
I i = Impl::get;
When I call:
i.get(null);
I get a NullPointerException:
Exception in thread "main" java.lang.NullPointerException
Can someone explain why this happens even though the Impl reference is not used anywhere?
I think you misunderstood the meaning of this line:
I i = Impl::get;
I is a functional interface that represents a method that takes an Impl and returns a boolean, whereas get is a method that takes no parameters and returns a boolean. How does this conversion work? Well, the compiler realises that get is an instance method, and to call it you must need a Impl object. Isn't that just like a function having a parameter before it is called?
So the compiler can happily infer that you meant:
I i = impl -> impl.get();
Now the cause of the NPE should be clear.
In general, all instance methods can be thought of as static methods that take one extra parameter, of type T where T is the declaring type of that instance method.

What do the instances of "this" mean in this code? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class MyResults extends Results {
...public MyResults() {
this(5);
}
public double average() {
return this.getSum()/numberOfCourses;
}
}
What do both instances of ―this mean in the code?
First instance is a call to another constructor in the same class. This is also known as Constructor Chaining pattern. Since you didn't post the entire code we don't know if that other constructor is defined (it should be, otherwise you'll have a compile time error).
Second instance is a call to the getSum() method. This method might be defined either in MyResults class or Results class (or some parent class of Results, if any).

Can we define this keyword as a reference variable in Java? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class Temp {
private int x = 3;
public void show() {
this.x = 4;
this.show(); // same as show();
}
}
Can we say that this is a reference variable ?
From the Java Language Specification:
[...] the keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked [...]
So this is not a reference variable, it is in fact a keyword. From the above description you could say it behaves like a reference variable (or better said a constant since you can't change it) if it is used in a certain context.
this is a reference indeed, however it is a constant, thus you can't change its value. this always references an object that object method was invoked on.
Yes. it is in fact a reference variable which refers to the current object.

Why instance method cannot override the static method [duplicate]

This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Closed 5 years ago.
class Abc{
public static void hello(){
System.out.println("parent");//Line1
}
}
class Abc1 extends Abc{
public void hello(){//Line2
System.out.println("child");//Line3
}
}
Compiler gives error at line 3 saying that
This instance method cannot override the static method from Abc
Why can static methods not be overridden by a instance method?
Simple: because the language specification says so.
That is one of the downsides of static methods: there is no polymorphism for them! Conceptually, they are not meant to be overridden. That is all there is to this.
To be precise: the JLS says differentiates between static and non-static method and states:
An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.

Categories