Results 1 to 3 of 3
  1. #1
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default local variable tf1 is accessed from within inner class; needs to be declared final

    The error shows while compiling the below java code

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.border.TitledBorder;
    
    public class m extends JFrame{
      JFrame frm1 = new JFrame("Menu");
      Container frm=getContentPane();
    
      m() {
      super("Main Menu");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 600);
        frm.setLocation(300, 300);
        //frm.getContentPane().
        int off_x;
        int off_y;
        off_x=140;
        off_y=45;
    
        
         final JPanel p1 = new JPanel();
        TitledBorder title;
    title = BorderFactory.createTitledBorder("Group 1");
    p1.setBorder(title);
    
      JTextField tf1 = new JTextField("./");
      JTextField tf2 = new JTextField("./");
      
      frm.add(p1);
      p1.setBounds(off_x-130, 10+off_y, 270, 122);
      
      
        p1.setLayout(null);
        frm.setLayout(null);			//menu
        JButton btn1 = new JButton("Go");
        JButton btn2 = new JButton("Button2");
        JButton btn3 = new JButton("Button3");
    
    p1.add(tf1);
    tf1.setBounds(10, 20, 115, 29);  
        p1.add(btn1);
        btn1.setBounds(130, 20, 130, 29);
    
    p1.add(tf2);
    tf2.setBounds(10, 50, 115, 29);  
        p1.add(btn2);
        btn2.setBounds(130, 50, 130, 32);
    
    
        btn1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            String txt;
            txt = tf1.getText();
    
            String line = null;
    
            try {
              Process p = Runtime.getRuntime().exec("xterm -e"+txt);
    //	  Process p = Runtime.getRuntime().exec("xterm -e ./t.sh"); 
            
            }
    catch (IOException e) {  
      e.printStackTrace();  
       }  
        ;
    
          }
        });
    
        
        frm.setVisible(true);
        show();
      }
    
      public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new m();
          }
        });
    
      }
    
    }
    Compile error:
    Java Code:
    shoeb@shoeb-desktop ~/jv2 $ javac m.java
    m.java:61: local variable tf1 is accessed from within inner class; needs to be declared final
            txt = tf1.getText();
                  ^
    Note: m.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    1 error
    shoeb@shoeb-desktop ~/jv2 $
    if I do not take input from the text field [txt = tf1.getText();] it is ok.


    i.e if use exec("xterm -e ./t.sh"); it is ok


    Please help me

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

    Default

    It's a technicality that bothers the latest version of Java itself: your inner local class object is created in the context of the outer class object; if the outer class object refers to a member of that same outer class object it should be visible in the inner class object (because we're talking about one and the same variable). What happens when a method terminates and the inner class object still exists? (most of the time they do). Those local variables should be kept in existence although they are local variables.

    Java's solution was, and still is, that you have to make those local variable 'final'. It is an indication to the compiler that that variable should be copied to somewhere else (if needed) when the method itself terminates.

    A consequence of being 'final' is that nothing can change the value of the variable anymore once it is assigned an initial value. (there is a clumsy way around: make that final variable of type T[] instead of T).

    For the time being: do as the compiler says and make that local variable a final local variable and assign it an initial value before you create that local inner class.

    kind regards,

    Jos

  3. #3
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Thanks a lot JosAH!
    It is ok after defining tf1 as final

    Java Code:
    final JTextField tf1 = new JTextField("./t.sh");

Similar Threads

  1. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 09:17 AM
  2. Replies: 2
    Last Post: 01-09-2009, 07:23 PM
  3. Replies: 3
    Last Post: 11-17-2008, 03:49 PM
  4. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 03:01 PM
  5. Replies: 2
    Last Post: 03-31-2008, 05:11 AM

Posting Permissions

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