Results 1 to 4 of 4
  1. #1
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default How to stop executing method

    I have a method called menu() which is the controlling method in the programm and this method calls other methods what i need is when a condition happens in any of the other methods(that are called by menu) happens to stop this methods and go back to the menu method()

    Java Code:
    //what i need
    //e.x
    add()
    //if some condition is false
    //stop executing this method
    //go back to menu()
    Java Code:
    //what i do
    add()
    //if some condition is false
    menu();
    but i know that what i do that i call the menu method but the add()method is still in execution!!

    thanx in advance!!!

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    If you called the add method from the menu method, then the menu method will continue to execute after the add method returns. You can put a return statement at any point in the add method to force this return to happen before the end of the add method is reached.
    Don't forget that the return will return the code execution to the point in the menu method from where the add method was called.

  3. #3
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default

    thank you for your reply!!!

    but isn't there other ways to do this??!!
    As i knew that using break or return statments to break the flow is not a good habbit that decrease code readabilty!!!
    Am i right?!?!?!

    and thanx again!!:D

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Other ways depend on what you are trying achieve. Actually sometimes break and return can make the code more readable.

Similar Threads

  1. Thread Stop() method not working
    By charan reddy in forum Threads and Synchronization
    Replies: 18
    Last Post: 10-03-2009, 03:01 PM
  2. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 04:39 PM
  3. How to stop thread from being jumping off the code without executing it.....
    By chiragkini in forum Threads and Synchronization
    Replies: 6
    Last Post: 01-22-2009, 03:38 AM
  4. Can you stop a gif? xd
    By Exhonour in forum New To Java
    Replies: 0
    Last Post: 01-16-2009, 08:44 PM
  5. executing a method in main class
    By rangers27 in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 05:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •