Repeat: Difference between revisions
Content added Content deleted
(Added implementation for 'fe' language) |
|||
Line 982: | Line 982: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
There are two ways to achieve this, one way is through ''reflection''. |
|||
<syntaxhighlight lang="java"> |
|||
import java.lang.reflect.Method; |
|||
public class Program { |
|||
public static void main(String[] args) throws ReflectiveOperationException { |
|||
Method method = Program.class.getMethod("printRosettaCode"); |
|||
repeat(method, 5); |
|||
} |
|||
public static void printRosettaCode() { |
|||
System.out.println("Rosetta Code"); |
|||
} |
|||
public static void repeat(Method method, int count) throws ReflectiveOperationException { |
|||
while (count-- > 0) |
|||
method.invoke(null); |
|||
} |
|||
}</syntaxhighlight> |
|||
<pre> |
|||
Rosetta Code |
|||
Rosetta Code |
|||
Rosetta Code |
|||
Rosetta Code |
|||
Rosetta Code |
|||
</pre> |
|||
<br /> |
|||
Or |
|||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
<syntaxhighlight lang="java">import java.util.function.Consumer; |
<syntaxhighlight lang="java">import java.util.function.Consumer; |