Results 1 to 6 of 6
  1. #1
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Question System.out.println problem

    Hi,
    I am trying to view the current values of one variable during execution of a code.

    Java Code:
    public class TestSum {
        
    static double sum( int n ) {
            
            
            if( n == 1 )
                return 1;
            else {
                 
                return n*n + sum(n - 1)*sum(n-1);
                [COLOR="Red"]System.out.println(n);[/COLOR]
                
            }
    }
    
    }
    But Eclipse says that this particular line is unreachable code. Does anybody know why?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    How can you do something after you've already returned from the method?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by luke View Post
    But Eclipse says that this particular line is unreachable code. Does anybody know why?
    Yup, your method has already returned so any code following the return statement can never be executed. The Eclipse compiler is right again.

    kind regards,

    Jos

    edit: *sigh* too slow again ...

  4. #4
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Thanks for the replies.
    Then how can I keep track of this variable during execution?

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    Why can't you print it BEFORE the return?

  6. #6
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Why can't you print it BEFORE the return?
    Yes, stupid me. Thanks, Norm.

Similar Threads

  1. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 08:52 AM
  2. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-18-2008, 11:06 PM
  3. System.out.println
    By Sniper-X in forum Advanced Java
    Replies: 10
    Last Post: 05-05-2008, 03:41 PM
  4. System.out.println
    By sunjavaboy in forum Advanced Java
    Replies: 3
    Last Post: 03-22-2008, 01:30 AM
  5. Help me with system.out.println
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:30 PM

Posting Permissions

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