One reason would be to get a specific order that methods are to be called. I would guess that pattern language would be used for this. This can be done in JavaScript but what is the solution in Java, can it be done?
Asked
Active
Viewed 97 times
-1
-
js requires to define methods in proper sequence because it is scripting language, so methods that are called from other methods must be defined earlier. Java doesn't have such requirement since it is compiled language. – Alex Salauyou Oct 21 '15 at 10:26
-
@Sasha Salauyou That's not true. Functions can be defined in any order if using the normal function definition form `function name() {}` because of hoisting. – plalx Oct 21 '15 at 12:50
-
@Mik You question is very unclear. "This can be done in JavaScript" ... What is the JavaScript solution? Perhaps that would help understanding the problem. – plalx Oct 21 '15 at 12:51
-
@plalx Although i could not see the original posting perhaps this is something like what I would like to see in Java, and I am beginning to think that it is possible: http://stackoverflow.com/questions/10693488/how-to-run-a-randomly-selected-function-in-javascript – Mik Seljamaa Oct 21 '15 at 17:02
-
Your question is still extremely unclear. I'll vote to close if you do not clearly describe what you are trying to achieve and provide an example of what you have tried. – plalx Oct 21 '15 at 18:59
2 Answers
0
if you want that the user calls a specific sequence of methods, put this workflow into a method and make this method the only accessible point of entry for the user.
otherwise there is no need in java to define a specific order of methods in a source file.

Emerson Cod
- 1,990
- 3
- 21
- 39
-
How to make sure other programmers maintaining the source keep that point the only accessible point? – Mik Seljamaa Oct 21 '15 at 11:40
-
you can't - everyone having control of the source can change it to his/her like (or e.g. via reflections someone can too do what he/she wants). Your intention of course must be clear to avoid unintended changes by other programmers. – Emerson Cod Oct 21 '15 at 12:02
0
This is just a sketch right now, have not tested yet!
- put all the method names in a String[]
- create a for loop to traverse the array
- Then substitute the methodname for the array member in the code in the next clue: How do I invoke a Java method when given the method name as a string?

Community
- 1
- 1

Mik Seljamaa
- 33
- 7
-
This doesn't solve your problem. In @Emerson's answer you ask "How to make sure other programmers maintaining the source keep that point the only accessible point?" I'm going to ask that same question if you think this is your solution. Just write one method that contains all of the function calls you want in the order you want them to be called; make this method public and the rest private; and put in a comment these must be called in a specific order. – Luminous Oct 21 '15 at 17:30
-
What if i do not know all the existing functions until they are defined at compile time for the array? Sorry but that may have been too confusing in the initial question, I think this option by @Luminous is not generic enough. – Mik Seljamaa Oct 21 '15 at 17:53
-
You mean run time. Not compile time. You're implying you want to use reflection to create your array of function calls. Why? What if you want to change the order? You'd have to change your mechanism that creates the array or have a new one that will do that for you, but how will that string changing function know what to do? Do these functions take any parameters? How are going to handle them? – Luminous Oct 21 '15 at 19:17
-
This is the most important question you need to ask yourself: Does my design need this much reflection just to guarantee the order of the functions are called correctly? If you say yes, why? – Luminous Oct 21 '15 at 19:17
-