Is there a similar way to declare a with-statement in Java (as in Javascript), or are there structural reasons why this would not be possible?
with(obj)
{
getHomeworkAverage();
getTestAverage();
getAttendance();
}
...is nice and easy. However, it would seem that method calls have to be chained to their object(s) every time in Java, with no such graceful shortcuts avaiable:
obj.getHomeworkAverage();
obj.getTestAverage();
obj.getAttendance();
This is very redundant, and especially irritating when there are many methods to call.
There is no direct equivalent of "with".
If the methods are instance methods, you can give the target object reference a short identifier for use in a block:
{
Student s = student;
s.getHomeworkAverage();
s.getTestAverage();
s.getAttendance();
}
If the methods are static, you can use "import static":
import static java.lang.Math.*;
public class Test {
public static void main(String[] args) {
System.out.println(sqrt(2));
}
}
No, there's no with statement or a similar construct in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With