What do "x.this" and "x.class" mean? (Android Studio) [duplicate] - java

This question already has answers here:
What does "qualified this" construct mean in java?
(2 answers)
What is a class literal in Java?
(10 answers)
Closed 3 years ago.
I know what "this" means, but what does it mean when you put it behind the name of a class, or if you put ".class" behind the name of a class as in the code below?
Someone please explain
Intent intent = new Intent(ActivityA.this, ActivityB.class);

ActivityA.this refers to this of the enclosing class (ActivityA).
ActivityB.class simply refers to the ActivityB Class reference.

It means this. It's useful for Lambdas, nested classes, etc..

Related

Java Instantiation Syntax [duplicate]

This question already has answers here:
Why can I create an variable with type of parent class
(2 answers)
What does it mean to "program to an interface"?
(33 answers)
Closed 12 months ago.
I am currently learning Java as a past time, and I am troubling in absorbing this instantiation syntax.
I know that if a Main class is created, I can create an object by using this
Main obj = new Main();
But in further lectures (specifically inheritance and polymorphism), they are creating objects as
Parentclass object = new Subclass();
I've tried using
Subclass object = new Subclass();
and have the same output.
Can you please tell me the difference about these two?

Why is new used to access the constructor of a class? [duplicate]

This question already has an answer here:
New object instantiation when using Java 8 streams
(1 answer)
Closed 2 years ago.
I came across a piece of code as follows:
final static Map<String, Supplier<Shape>> map = new HashMap<>();
map.put("CIRCLE", Circle::new);
Where Circle is a class. I guess here new is used to access the constructor of the class Circle. How can new be used like this? What is this technique called? I could not find any documentation.
This a reference to a constructor. You can pass that where a matching functional interface is expected. Look here for more info http://tutorials.jenkov.com/java/lambda-expressions.html#constructor-references

Java | What does synthetic mean? [duplicate]

This question already has answers here:
Synthetic Class in Java
(13 answers)
Closed 7 years ago.
I was going through the java Method.class (Decompiled) and I found something that caught my eye.
#Override
public boolean isSynthetic() {
return super.isSynthetic();
}
"Synthetic". What does that mean?
Is it usable in code?
.
And since I found this in the Method class, I was wondering, is it that an entire method could be "synthetic", or is it that it contains something synthetic?
Thanks in advance.
.
not a copy of What is the meaning of “static synthetic”?
Any constructs introduced by the compiler that do not have a
corresponding construct in the source code must be marked as
synthetic, except for default constructors and the class
initialization method.
http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html

Java Reflection methods hashmap [duplicate]

This question already has answers here:
Java: NoSuchMethodException when method clearly exists
(4 answers)
Closed 7 years ago.
I am trying to add static methods from an existing class to a HashMap. All the methods are located in the same class with the following code :
map.put("x", myClass.class.getMethod("addX"));
map.put("y", myClass.class.getMethod("addY"));
When I run the code I get java.lang.NoSuchMethodException: package.myClass.addX.
Any ideas?
You should use getDeclaredMethod() method instead of getMethod().

is setMnemonic a constructor or method in java? [duplicate]

This question already has answers here:
Methods vs Constructors in Java
(11 answers)
Closed 7 years ago.
is setMnemonic(int) a constructor or method?
http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
It is a method. A constructor will always consist of the name of the class being called on. This is a method, because it is called after an instance of some class, using the dot.

Categories