Can we Use variables declared in outer block | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we Use variables declared in outer block

i need to access variables declared outside the block ,so any ideas.i tried many methods but all seem to fail so my code contains to get a text entered by the user like –------------------------------------ JTextBox text=new JTextBox(); String name; {//action listener part on clicking a button { name=text.getText(); System.out.println(name);//error occurs } } --------------------------------------- so any ideas thanks in advance

13th Jun 2017, 1:30 PM
harish
harish - avatar
4 Answers
+ 2
Post the error message
13th Jun 2017, 3:19 PM
Da' BO$
Da' BO$ - avatar
+ 2
Can't see the error clearly, but try declaring the variables globally.
13th Jun 2017, 3:55 PM
Da' BO$
Da' BO$ - avatar
0
can't access elements of outer block
13th Jun 2017, 3:21 PM
harish
harish - avatar
0
Exception in thread"awt-eventqueue-0" java.lang.error cannot refer to a non final name inside an inner class defined in a different method tried posting it globally but didn't help
13th Jun 2017, 4:01 PM
harish
harish - avatar