Reputation: 1745
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?
Upvotes: 2
Views: 557
Reputation: 1743
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)
Upvotes: 1
Reputation: 5018
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)
Upvotes: 1
Reputation: 16898
The last "statement" is run, it's simply that you can't see the variable result because:
The variable doesn't exist before this statement.
The variable doesn't exist while the statement is being executed - the last step is to assign the resulting value to the variable.
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.
Upvotes: 2
Reputation: 115378
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. :)
Upvotes: 2