How to access method inside object of object through method refrence [duplicate] - java

This question already has answers here:
Java 8 chained method reference?
(1 answer)
Multiple lambda method references
(3 answers)
Closed 4 years ago.
There are two classes A & B. For example given below:
class A{
B b;
B getB(){ return b; }
}
class B{
String x = "Test";
String getX(){
return x;
}
}
I have object of A in .map(method_refrence_here_for_getX) and I want to call getX so there is any way to do it?
Although i can do like below:
.map(A::getB)
.map(B::getX)
or
.map( a -> a.getB().getX())
But I want to do something like .map(A::B::getX) I know it's absolutely wrong but I want something like this so anyone can help?

You simply can't. There is no syntax for that and it's called a method reference (method) and you potentially show a field reference, which is not supported and looks weird to begin with IMO.
There is a difference between the two (map(A::getB).map(B::getX) and map(a -> a.getB().getX())) is that you will save an additional method internally, but this is one tiny optimization and you should really look into what is more readable in your opinion.

Related

how do i call between java classes? [duplicate]

This question already has answers here:
Calling static method from another java class
(3 answers)
Closed 3 years ago.
I am new to Java and still trying to understand the basics. I can write code within a class and method but really struggling with working across classes. To help I am trying to write a very simple programme that can add or subtract two numbers. This program will have three classes:
Class 1
package christmas;
public class addintegers
{
public static int add2numbers(int a, int b)
{
return (a+b);
}
}
and the second class
package christmas;
public class subtractintegers {
public static int sub2numbers(int a, int b)
{
return (a-b);
}
}
what I now want to do is to write a third class that contains a main method that can read two numbers and an operator (+ or -) from the keyboard and then depending on the operand call the addintegers or subtractintegers classes.
This seems basic and it is not homework, I am simply trying to understand through a basic example how to build more complex code to work across mumtiple classes.
I know how to read from the keyboard, the help I need is how to call the different classes from a main method that is in a class of its own.
Thank you in advance - I am sure this is easy for many but hard for me to understand.
To call the christmas.addintegers.add2numbers method from another class, you just do christmas.addintegers.add2numbers(foo, bar) (where foo and bar are some already initialized integers.)

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).

Find the caller of a method (reflection) in Java [duplicate]

This question already has answers here:
How do I find the caller of a method using stacktrace or reflection?
(13 answers)
How to get the caller class in Java [duplicate]
(10 answers)
Closed 9 years ago.
I want to recover in Java the instance of the caller.
Here is my code
class A {
void methodA() {
B b = new B();
A a = b.methodB(); //needs to be the same instance ie "this"
}
}
class B {
A methodB() {
//Here the code (with reflection) I need
A a = getPreviousInstanceCaller();
return A
}
}
Does it exist in Java a way to do that ? Maybe with reflection ?
You don't need reflection for this. You need one of these methods.
http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getStackTrace%28%29
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getStackTrace%28%29
Note that the ordering of the stack trace elements (0, 1, 2, etc.)
may vary in different versions of the JDK. Meaning in some versions
element 0 may be the top-most element, while in others it may be the
bottom-most one. This is an important thing to have in mind.
See here for more details.
Getting the name of the current executing method

What is a Type Literal in Java? [duplicate]

This question already has answers here:
Use of TypeLiteral in java
(5 answers)
Closed 9 years ago.
There is an ASTNode subclass called TypeLiteral.
The description of this type of code says that it looks like
( Type | void ) . class
I don't believe I have ever seen anything like this.
Can anyone provide an example of a TypeLiteral, and perhaps additionally of it being used in a Java program?
There is a class called Class that represents the class of an object. It contains various useful methods for introspection on that class and objects of that class. One way to get a class object is to use the Object method getClass(). But if you want to refer to a Class by name of its class, you use a class literal.
Some examples:
class Foo {
void method(Object b) {
if(b.getClass() == Foo.class) {
Foo f = Foo.class.cast(b);
}
Foo.class.getResourceAsStream(...);
Foo.class.getMethod("method", Object.class);
}
}

Invoking a method by its name [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I invoke a Java method when given the method name as a string?
I have 10 methods named: m1, m2, m3,...
like this:
public void m1(){
..
}
How do I invoke them with string in a 'for' loop?
I want to do this:
for (int i=1;i<11;i++){
invoke('m'+i);
}
You need to use reflection to achieve this.
Method method = getClass().getMethod(methodName);
method.invoke(this);
So, you need to store your method names in an array and use this code piece to call those methods one by one.
You can do this with reflection.
However, I would be interested in your use case. Often it is possible to refactor an application, so that the use of reflection is superfluous.
Use java reflection on this object.

Categories