try catch blocks and the effect on variables

In my main function I have a series of try/catch blocks to surround various parts of my code. Unfortunately, the compiler is complaining when I try to use a variable defined in a different try-catch block. It says "cannot find symbol", where the symbol is the variable name. For example,

public class theClass {

public static void main(String[] args) {

try {

int fruity = 1;

} catch (Exception e) { }

try {

fruity = 2;

} catch (Exception e) { }

}

}

The compiler throws an error like "cannot find symbol, symbol: variable fruity".

I presume I'm misunderstanding how the try-catch block works? Any help is appreciated. Thanks!

[704 byte] By [Feldon] at [2007-9-30 22:09:38]
# 1
A try/catch/finally statement creates code blocks with the curly braces. These blocks have the same effect as any other code blocks. If the same-named variable is used in two try blocks, you can define it outside of either block.
ChuckBing at 2007-7-7 11:22:45 > top of Java-index,Security,Event Handling...
# 2

try {

int fruity = 1;

}

your integer 'fruity' ...only exists between the { after try and the closing brace } - it is local to the scope of the block

so when you then say

try {

fruity = 2;

}

there is no variable called fruity that exists

The-Sue at 2007-7-7 11:22:45 > top of Java-index,Security,Event Handling...