In Java 8, there are situations where I would like for the program to point to a specific part of a method rather than at the start of the method, but is there a way to do this?
I know that, for example, I could create a loop with a copy of the section of code I want to repeat, and I could also break the method up into sections such as:
public static void main(String[] args)
{ //do stuff
methodA();
}
public static void methodA()
{
//do more stuff
methodB();
}
public static void methodB()
{
//do some other stuff
if (answer == 1) methodA(); //return to methodA
else System.exit(0);
}
This is a very simplistic example (with obvious code missing, but is just shown to reference) of how I have been going about it, but I would love to be able to insert some kind of label within main and reference to it like a method without having to break up the main method into chunks.
The question is: Is it possible? Or is there some other approach I could be taking?
Also: If this is not available in Java, but is available in some other language, a reference to such a language would be helpful for me to look into.
EDIT: To clarify, I am not specifically looking for a goto() command so please don't assume such in your answers. I also would prefer avoiding a goto() command even if available because anything that points to a line rather than a method will break if I ever add code above it. What I was imagining was more along the lines of this:
public static void main(String[] args)
{ //do stuff
label bookmarkA();
//do more stuff
bookmarkA();
}
As you can see, this is declaring where I would be jumping to later and then calling it like I would be calling a method. Based on the answers below, I doubt something like this specifically exists (although I would love it if it did), but it seems that there are some commands to move up or down within a method within some limits.
A good answer might tell me what is possible and at least point me in a direction to properly use it. A good answer might also tell me if something closer to what I want is also available in another language.
Lastly, this is not a duplicate of questions asking for goto() commands because that's not really what I want.
EDIT 2: In case anything about this question is unclear, I am not asking about any SPECIFIC approach to doing what I want, nor am I specifying that I want to begin execution of a method anywhere other than at the beginning of the method I am starting in. I am asking, specifically, what is possible. Please assume that there is only ONE method in use or that everything I would like to happen is in the same method.
As I explained VERY CLEARLY, I already know how to approach the problem with multiple methods, but I wanted to solve the issue within the same method if possible.
Based on the comments received so far, this is possible with limitations.
1.) The goto() approach does not function in Java 8. Of course, that wouldn't be a solution to the problem anyway because pointing to a specic line can cause problems if anything is ever added above such a reference later. The alternative idea also does not appear to exist.
On the other hand, limited functionality of goto() in java was implemented by this approach.
2.) As another has pointed out, the method has to run from the beginning at least once. Of course, since the question is focused on trying to solve the problem without creating additional methods this should go without saying. The method is already running and I only want to move up within the same method.
3.) The available functionality seems to be limited to break, break , continue, continue . Although I have not received more details, it seems to be a good direction to go from for trying to approach my problem with.
4.) It also appears to be possible to make some use of assembly within Java. You can apparently call C code via JNI and use the C code to call assembly which would allow for the goto() functionality if absolutely needed. Since the assembly instructions would be kept separate, likely in it's own class, it shouldn't lead to spaghetti code and would avoid some of the issues of using goto() normally.
I prompted for others to create such a listed answer, but no one has so I felt it necessary to answer it myself to avoid misinformation to be passed to others. If, however, someone has a more complete answer within the next few days I will select their answer as the best.