Results 1 to 6 of 6
  1. #1
    Javasubbu is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Trouble accessing outer class variable from Inner class

    Hi,

    The program deals with frame and intended to established jdbc connectivity to insert name and age into MySQL. The problem I face is accessing outer class variable from inner class. Any suggestions.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    class frametest2 extends JFrame{
    public static void main(String args[]){
    final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    final String DB_URL = "jdbc:mysql://localhost:3306/test";
    final String USER = "root";
    final String PASS = "test123";
    JFrame f = new JFrame("Customer details");
    JPanel p = new JPanel(new GridLayout(2,2,10,10));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel name = new JLabel("Enter the name of the customer");
    JLabel age = new JLabel("Enter the age");
    JTextField tname = new JTextField(15);
    JTextField tage = new JTextField(15);
    JButton b = new JButton("Add");
    p.add(name);
    p.add(tname);
    p.add(age);
    p.add(tage);
    p.add(b);
    f.add(p);
    b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    try{
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(DB_URL,USER,PASS);
    System.out.println("Creating statement...");
          stmt = conn.createStatement();
          String sql;
    String dbname = tname.getText();
    int dbage = Integer.parseInt(tage.getText());
    sql = "insert into test1" + " values( '" + dbname + "'," + dbage + ")";
    }catch(Exception e){System.out.println("Exception caught " + e);}
    }
    });
    f.pack();
    f.setVisible(true);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble accessing outer class variable from Inner class

    Suggestion:
    • Out of respect for the volunteers who might help you, please only post properly indented code. If you ask us to put the effort into trying to understand your code and then helping you, it's not asking you too much to request that you put the effort into not making this extra difficult for us to do.
    • Which variable are you having trouble with? You never say.
    • Often this problem is resolved by either making the variable final or by declaring it in the class.
    • You probably want to get most of the code in your main method out of the main method and into OOP-land.


    Luck!

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Trouble accessing outer class variable from Inner class

    Based upon your title only, since I won't even try to dissect the code you posted, you can access an outer class instance specifically by using the ClassName.this syntax

  4. #4
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Trouble accessing outer class variable from Inner class

    Where is conn declared? What is its type?
    Last edited by 2by4; 12-17-2011 at 09:31 PM.

  5. #5
    Javasubbu is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: Trouble accessing outer class variable from Inner class

    Hi,

    Regret the trouble caused. below is the code. And I get error in line 35,36.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    class frametest2 extends JFrame{
    public static void main(String args[]){
    final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    final String DB_URL = "jdbc:mysql://localhost:3306/test";
    final String USER = "root";
    final String PASS = "test123";
    JFrame f = new JFrame("Customer details");
    JPanel p = new JPanel(new GridLayout(2,2,10,10));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel name = new JLabel("Enter the name of the customer");
    JLabel age = new JLabel("Enter the age");
    JTextField tname = new JTextField(15);
    JTextField tage = new JTextField(15);
    JButton b = new JButton("Add");
    p.add(name);
    p.add(tname);
    p.add(age);
    p.add(tage);
    p.add(b);
    f.add(p);
    b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    try{
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Connecting to database...");
    Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
    System.out.println("Creating statement...");
          Statement stmt = conn.createStatement();
          String sql;
    String dbname = tname.getText();
    [U]int dbage = Integer.parseInt(tage.getText());
    sql = "insert into test1" + " values( '" + dbname + "'," + dbage + ")";[/U]
    }catch(Exception ae){System.out.println("Exception caught " + ae);}
    }
    });
    f.pack();
    f.setVisible(true);
    }
    }
    The error reported is
    XML Code:
    frametest2.java:34: error: local variable tname is accessed from within inner cl
    ass; needs to be declared final
    String dbname = tname.getText();
                    ^
    frametest2.java:35: error: local variable tage is accessed from within inner cla
    ss; needs to be declared final
    int dbage = Integer.parseInt(tage.getText());
                                 ^
    2 errors

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Trouble accessing outer class variable from Inner class

    If a method creates an inner class, and that inner class attempts to access variables declared in the method (and lose scope after the method exits), said variables must be declared final. In situations where these variables do not change when the method is called (in your case main), you may be better off declaring them as instance or class variables (eg static).
    See http://docs.oracle.com/javase/tutori...classvars.html

Similar Threads

  1. Accessing session variable from Java Class
    By Rohit Pokhrel in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-08-2010, 04:07 PM
  2. Replies: 6
    Last Post: 03-31-2009, 04:27 AM
  3. Replies: 0
    Last Post: 02-17-2008, 09:03 AM
  4. Replies: 0
    Last Post: 02-17-2008, 09:01 AM
  5. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 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
  •