2
public static void main(String args[])
{
  if(true)
    {
      int x= 3*44+7;
      //int y=1;
    }
}

I narrowed my problem to this simple statement and I dont really know how this variable can be accessed in the eclipse debugger. It always happens in situations where a variable is declared in a if condition, try-catch statement, loops, etc and is accidentally the last statement in that block.

To solve this issue i stop my debugging session, add another statement to that block, redo everything I just did. Is there a better solution?

M.C.
  • 1,765
  • 3
  • 29
  • 31
  • 3
    I'm a visual studio/.NET guy and when debugging the curly bracket is considered the last step of the statement. Too bad it's not the case in java/eclipse apparently – Laurent S. Jun 05 '13 at 14:44
  • Also see Method breakpoints at duplicate SO question: http://stackoverflow.com/questions/7785887/cant-breakpoint-the-last-statement-of-a-code-block-in-eclipse – Stefan Feb 01 '17 at 17:49

4 Answers4

2

You are right, the it is hard to see the value of the last statement: when you pass the last line the program terminates.

The workaround can be either to add dummy line after this statement and put breakpoint there or to use "Expressions" view and put expression of x (i.e. 3*44+7) there.

BTW please pay attention that this is not a typical case in real world where programs are a little bit longer than 1 executable line. :)

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 2
    it has nothing to do with one line.. It has to do with the last line in any block – M.C. Jun 05 '13 at 14:44
  • @M.C. A variable that is declared on the last line of its block is never live. `int x= 3*44+7;` as the last line of a block has no side effects and its result is dead, so executing it would be a waste of processor cycles. – Patricia Shanahan Jun 05 '13 at 15:16
  • @PatriciaShanahan This is a good point, but as a Debugger(er) I would have wanted this statement to be executed for Debugging purposes. As previously commented, VS seems to put that into consideration. – M.C. Jun 05 '13 at 15:21
  • @M.C. Rather than spending time and effort debugging it, why not delete the dead declaration? – Patricia Shanahan Jun 05 '13 at 16:14
  • @PatriciaShanahan I do the declaration when I am sill in the testing phase and I use the Inspect view that comes up when hovering through an object to get more information on that object. If I release the application and still have that last line of code then executing it will be a waste of processor cycles. As the answer of this question indicates I use now the Display window to avoid that problem. But I still rather have the inspect view of any object while testing instead of having to type something that I can get by just hovering. – M.C. Jun 05 '13 at 16:43
2

The last "statement" is run, it's simply that you can't see the variable result because:

  1. The variable doesn't exist before this statement.

  2. The variable doesn't exist while the statement is being executed - the last step is to assign the resulting value to the variable.

  3. The variable would have existed on the next line, but that line ends the scope that the variable is declared in, so the variable no longer exists.

In a more "real world" example, you could do something like this -

Change:

public int doIt() {
    return 3*44+7;
}

To:

public int doIt() {
    int x = 3*44+7;
    return x;
}

And set the breakpoint on the 'return' line.

Nate
  • 16,748
  • 5
  • 45
  • 59
  • Another option, in eclipse, would be add the operation to the "Expressions" view (selecting the code in the editor once you are debugging and then right click and select 'Watch' – Pablo Lozano Jun 05 '13 at 14:54
  • @Pablo the Expression view is a useful view that was unfamiliar to me but unfortunately it does not solve this issue – M.C. Jun 05 '13 at 15:02
1

I can't see any solution to your problem. Since you can only see variables defined before your current line, you obviously need a statement after your variable declaration. If there's no statement, your variable is not in current scope anymore (in your case, your program ends) and thus cannot be queried.

BTW, you said that you stopped your debugging session. With HotSwap, you could have dynamically replace current method's code and restart your debug at the beginning of the method (see 'Drop to frame' in your debugger)

Guillaume Darmont
  • 5,002
  • 1
  • 23
  • 35
  • Drop Frame is a useful feature I was not aware of, but I was unable to add additional statements and I read it does not allow everything to be changed. As you already said, there is probably no solution – M.C. Jun 05 '13 at 15:18
1

In your situation the compiler might actually remove the assignment, as the variable x is never used later on.

Anyways... one workaround you can use in your debugger, (assuming the statement you wish to debug is not state changing) would be to use the scrapbook, or to use inspect.

http://www.ibm.com/developerworks/library/os-ecbug/ figures 7 and 8

you can highlight an expression (part of a statement or entire statement) and inspect I believe. (haven't used eclipse in a few months). the alternative is to stop at the line (so before the expression triggers) and copy the line into your display view, and run it there.

it will run within the current stackframe, so all your local objects are available. however , running set and other state changing calls will actually change the state of your program. (it's not ideal but it beats stopping the debugger)

Joeblade
  • 1,735
  • 14
  • 22
  • Using the Display you mentioned in your link solved my problem. I was unaware that there exists a window in eclipse to execute immediate code. And using that window I can avoid restarting my whole application for adding a dummy line! – M.C. Jun 05 '13 at 15:46