Can I use a double colon two times?
Sample I have 3 class
Class A {
String name;
B classB;
}
Class B {
String age;
C classC;
}
Class C {
String location;
}
I need to use A::getClassB::getClassC::getLocation
Is this possible?
Can I use a double colon two times?
Sample I have 3 class
Class A {
String name;
B classB;
}
Class B {
String age;
C classC;
}
Class C {
String location;
}
I need to use A::getClassB::getClassC::getLocation
Is this possible?
I need to use
A::getClassB::getClassC::getLocation
Is this possible?
Not as such.
You're talking about method reference syntax, which designates a method via its name and either the name, the type, or an instance of a class or interface to which it belongs. According to the Java Tutorial, there are exactly four forms they can take:
ContainingClass::staticMethodName
containingObject::instanceMethodName
ContainingType::methodName
ClassName::new
Type and variable names cannot contain colons, so the only one of those that could conceivably afford chaining colons would be (2), where you would then be forming a reference to a method of a lambda. That does not appear to be what you want to do.
If your classes actually had getter methods such as you seem to want to use, then you should be able to write a lambda similar to your expression:
a -> a.getClassB().getClassC().getLocation()
Alternatively, you should be able to write a method reference of this form:
myA.getClassB().getClassC()::getLocation
Those have different significance, as this example code demonstrates:
import java.util.function.Function;
import java.util.function.Supplier;
public class X {
// Note the types declared for these objects:
Function<A, String> f1 = a -> a.getClassB().getClassC().getLocation();
Supplier<String> f2 = new A().getClassB().getClassC()::getLocation;
}
class A {
String name;
B classB = new B();
public B getClassB() { return classB; }
}
class B {
String age;
C classC = new C();
public C getClassC() { return classC; }
}
class C {
String location;
public String getLocation() { return location; }
}
Note in particular that a -> a.getClassB().getClassC().getLocation()
is a lambda that that computes a location string for an A
given as input, whereas myA.getClassB().getClassC()::getLocation
is a reference to the getLocation()
method of the particular C
obtained by invoking myA.getClassB().getClassC()
(at the point where the method reference is instantiated, not the point where it is used). I suspect that one of those is what you actually want.