Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Unreachable statements

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Location
    Sweden
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unreachable statements

    Is it somehow possible to make the compiler not to care about unreachable statements? Wouldn't a warning be enough?

    Best regards
    Marcus


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Unreachable statements

    in my opinion , i dont think so... because the compiler will execute and read sequentially each and every line of your code.. even the smallest space(i think so)...

    so if the compiler encounters anything that IT CANT REACH... it will complain... because it cant reach it..

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Unreachable statements

    consider this as an example


    lets assume that you make a method with a switch structure..

     
     
    public  int myMethod() {
     
     switch (value) {
     
       case <value1>:
                         break;
                         return 0;


    of course the compiler will complain.. AND it wont be able to execute the program..
    because how can it return something if the program already breaks?....

    it wont be able to reach the return value...
    Last edited by chronoz13; December 8th, 2009 at 10:00 AM.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Unreachable statements

    Whats the point in keeping unreachable code anyways?

    // Json

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Location
    Sweden
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unreachable statements

    Quote Originally Posted by Json View Post
    Whats the point in keeping unreachable code anyways?

    // Json
    During the creation of a program I can see plenty of good reasons for temporarily wanting to have unreachable code. You may for example want to switch off large parts of a function with a simple return statement instead of jerking around with commenting stuff out. It's kind of a useful debug-thing.
    Last edited by Marcus; December 8th, 2009 at 12:17 PM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Unreachable statements

    There's no way around this in Java unless you choose to write your own Java compiler. There are ways to "trick" the compiler into thinking that a section of code is reachable when it is really unreachable, but I would strongly recommend AGAINST doing this because there really isn't any point in doing so and will greatly confuse anyone who is trying to read your code.

    The best way to test segments of code is to build them up separately, test them against expected (and maybe unexpected) inputs to see if they work, then stuff them into your main code. Once you know that a section of code works alone, you can be guaranteed that that section of code will function exactly as it should, and if any problems occur it is either from incorrect use of that code by the main code, or is simply somewhere else.

  7. #7
    Junior Member
    Join Date
    Dec 2009
    Location
    Sweden
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unreachable statements

    Quote Originally Posted by helloworld922 View Post
    There's no way around this in Java unless you choose to write your own Java compiler.
    Well, actually I've written a programming language that creates windows applications. I'm now working on a version of the compiler that creates Java applets instead. I know it's probably not of any one's interest. But the language can be found at NaaLaa (link). I've compiled one of the examples to a Java applet that can be found at http://www.naalaa.com/breakout_java/test.html (link). It's a breakout-clone that takes very long to load (the images are all png and copied directly from the windows app), but it works very well (although slow) so far :-)

    However, my language doesn't mind unreachable code. Therefor it can also generates Java code with unreachable statements. It'd probably take me forever trying to make the compiler detect such code segments ...
    Last edited by Marcus; December 10th, 2009 at 12:55 PM.

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Unreachable statements

    Why not give assertions a try then?

    // Json

Similar Threads

  1. need help with JButton and switch statements
    By jjoubert in forum AWT / Java Swing
    Replies: 5
    Last Post: October 28th, 2009, 09:13 AM
  2. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM
  3. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 12:15 PM
  4. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 10:23 AM
  5. unreachable statement?
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 11th, 2009, 10:06 AM