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.