Frage

Given the following code, how can I use myInt in private void jButton1 from private void jButton2?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int myInt = (Integer)jSpinner1.getValue();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int b = 0;
    int c = myInt;
    do {
        try {
            Object newInstance = Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/rk","root","root");
            PreparedStatement ps = (PreparedStatement) con.prepareStatement("INSERT INTO factors VALUES(?,?,?,?,?,?)");
            ps.setString(1,jTextField7.getText());
            ps.setString(2,jTextField8.getText());
            ps.setString(3,jTextField9.getText());
            ps.setString(4,jTextField10.getText());
            ps.setString(5,jTextField11.getText());
            ps.setString(6,jTextField12.getText());

            ps.executeUpdate();

            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField10.setText("");
            jTextField11.setText("");
            jTextField12.setText("");
        }
        catch (Exception e) {
            System.out.println(e);
        }

        b++;
    } while(b < my);
}
War es hilfreich?

Lösung

Variables are tied to a Variable Scope. In this case, myInt is declared in one method scope and you are then trying to access it from a different method scope. This is not possible.

To go around it, you will have to elevate the myInt variable to a Global (Class) variable, like so:

private int myInt;
...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    this.myInt = (Integer)jSpinner1.getValue();
}
...

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   int b = 0;
   int c= this.myInt;
...

The will mean that anything declared within the class scope will have access to this variable, meaning that your variable is now accessible from all your methods.

Andere Tipps

You can't. These are local variables which only exist for the duration of the method call.

If you want to use the value in another method, you've either got to pass it in through parameters, or make it an instance (or static, if you must) variable so that it's part of the state of the object.

Basically, your jButton1ActionPerformed method has no useful purpose at the moment - it's assigning a value to a local variable which then immediately goes out of scope.

As an aside, if you're unfamiliar with the basics of Java I would strongly advise you to learn them in console applications, where you don't need to deal with the complexities of user interfaces. Learn the basics of the language, then some of the core libraries (collections, IO etc) then start on user interfaces.

pass myInt to a field.

private int myInt;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    myInt = (Integer)jSpinner1.getValue();
}


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int b = 0;
    int c=myInt;
  ...
}

Even if you had your method public, you would not have been able to access it's local variable outside.. It's scope is limited to the defining method only..

Its better you declare that variable as field in class..

Variable myInt in your method jButton1ActionPerformed should be declared as instance field, else there won't be any use of that variable.. You are not using it anywhere..

the only way is to pass the local variable as an argument to the other private method. Local variables(variables declared inside the method) are confined to that method only. unless u pass them as argument or return them you cant use them directly in other methods. and yeah, it doesnt matter if its a private/public/protected/default/static marked method.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top