获取另一个类中变量的值?

时间:2013-09-28 14:11:53

标签: java

我在将变量传递给另一个类时遇到问题,因为它不断传递给我一个空白变量。

我希望从方法id获取search()的值,并将其转移到方法class foo内的total ...

班级Boo

//method inside of class boo

public void search(){

try{

        String id = searchBox.getText();
        String idNum="";

        rs = stat.executeQuery("Select * from students where idNum='"+id+"'");

        while(rs.next()){

            idNum = rs.getString("idNum");

        }//while

            Members members = new Members();
            setVisible(false);

            members.setIdVal(id);

    }catch(Exception e){
        System.out.println("Error: "+e);
    }

}//search

班级Foo

// methods inside Foo
public void total(){

    System.out.println("Get: "+getIdVal());

    try{

    rs2 = stat2.executeQuery("SELECT * FROM paymentRecord where idNum ='"+getIdVal()+"';");

    }catch(Exception e){
        System.out.println("Total Error: "+e);
    }//

}//total

public void setIdVal(String val){

    this.val = val;

}//get the id

public String getIdVal(){

    //System.out.println("Inputted ID:" + val);
    return this.val;

}//get the id

1 个答案:

答案 0 :(得分:0)

getIdVal()Foo类的方法,但它应该是Boo类的方法。

class Boo {
   private String id;
   public void search(){
        // ...
        this.id = searchBox.getText(); 
        // ...
   }

   public int getIdVal() {
      return this.id
   }
}

Foo课程:

Class Foo {
   public string Total(){
      Boo boo = new Boo();
      System.out.println(boo.getIdVal()); // will print the value from id searched in search() method
   }
}

您必须选择调用search()方法的位置。