5


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?


For example, this Javascript:
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.


  • So, is there any similar way to declare a with-statement in Java?
  • And if this is not possible, what are the reasons that it is possible in Javascript as compared to not possible in Java?
Ian Campbell
  • 2,678
  • 10
  • 56
  • 104
  • What are you using the `with` statement for? You know, is one of those things in JS that is better not to use if possible, and that's 99% percent of the time. – elclanrs Dec 13 '12 at 06:46
  • I just *like* it, and it makes coding several method calls easy (which does not happen very often though). – Ian Campbell Dec 13 '12 at 06:50
  • 1
    On a side note, `with` is one of those "bad things" about Javascript - some [recommended reading](http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/) – Krease Dec 13 '12 at 08:18
  • Thanks @Chris, so I see from the above link that Douglas Crockford is showing that assigning variables in a with statement is ambiguous and unpredictable, and so should be avoided. Also, Crockford explains this in detail on p. 110 of the book *Javascript: The Good Parts*. – Ian Campbell Dec 23 '12 at 21:30
  • Another good argument explaining why the `with` statement is bad in Javascript is that it is lexically an expensive operation (see http://stackoverflow.com/questions/61552/are-there-legitimate-uses-for-javascripts-with-statement/1463937#1463937 ). However, many people yet still argue that the `with` statement is useful for readability, and its' expense is supposedly negligible when used for simplifying method calls. I do understand that it should *never* be used to assign variables though. – Ian Campbell Dec 23 '12 at 21:35
  • 1
    Look at this code snipped: https://gist.github.com/mojo2012/da4e8193d25b1cb92bf416493022a09d. I created a little helper method – mojo2012 Jul 05 '19 at 10:09

5 Answers5

10

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));
  }
}
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • 3
    +1 for import static, which is probably the closest similarity one would get to `with`. – Krease Dec 13 '12 at 08:16
  • 1
    +1 Thanks @Patricia, I have never heard of using `import static` before. Oracle suggests to use it sparingly, saying that it could be confusing when importing from several different classes to know which method came from which class (see http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html ). However, I can see it being very useful, *especially* when importing the `Math` class as you have shown. – Ian Campbell Dec 23 '12 at 21:46
  • 2
    @IanCampbell I think the same warning against excessive use should apply to "with" in languages that support it. – Patricia Shanahan Dec 24 '12 at 03:13
  • +1 `import static` works with enums as well. – Brian McCutchon Mar 22 '14 at 05:56
8

No, there's no with statement or a similar construct in Java.

Codo
  • 75,595
  • 17
  • 168
  • 206
6

If the class of obj is under your control, you could provide a Fluent interface, basically returning this in every function. This would let you chain method calls like this-

obj.getHomeworkAverage().getTestAverage().getAttendance();

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • This is awesome, very interesting and useful, thanks @Karthik. I have looked into this, and have found a good example here: http://hilloldebnath.byethost3.com/2009/08/20/implementing-the-fluent-interface-approach-in-java/ – Ian Campbell Dec 23 '12 at 21:36
4

There is a reason why you can't do this in Java. Perhaps the most obvious is that functions are not first-class objects in Java and therefore you cannot simply have a name referring to the function - it must be under a class. As mentioned by Karthik T, the way you would shorten this could be just creative use of whitespace:

obj
    .meth1()
    .meth2()
    .meth3()

where each method returns the object.

For more info on First-Class Functions: wikipedia

Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60
1

So, is there any similar way to declare a with-statement in Java?

No, there is not. The closest thing would be the import static mechanism described in Patricia Shanahan's answer.

And if this is not possible, what are the reasons that it is possible in Javascript as compared to not possible in Java?

They're two entirely different languages with different features/strengths/weaknesses. An analogy: A hammer and a screwdriver are both tools, but they are used in different ways.

Krease
  • 15,805
  • 8
  • 54
  • 86