[英]Calling a method?
okay. 好的。 I wrote the above code and I need to call it from another class.
我写了上面的代码,我需要从另一个类中调用它。 how can I do it?
我该怎么做? plus it gives me this error with
DefaultTableModel prodt = (DefaultTableModel) protable.getModel();
再加上
DefaultTableModel prodt = (DefaultTableModel) protable.getModel();
给我这个错误DefaultTableModel prodt = (DefaultTableModel) protable.getModel();
. 。 the error is non-static variable protable cannot be referenced from a static context.
错误是非静态变量protable不能从静态上下文中引用。
public static void refreshProtable() {
try {
Statement s1 = Db.connectDb().createStatement();
ResultSet rs1 = s1.executeQuery("SELECT * FROM product WHERE status='" + 0 + "'");
DefaultTableModel prodt = (DefaultTableModel) protable.getModel();
while (rs1.next()) {
Vector v1 = new Vector();
v1.add(rs1.getString("pid"));
v1.add(rs1.getString("pname"));
v1.add(rs1.getString("sp_rt"));
v1.add(rs1.getString("sp_wh"));
v1.add(rs1.getString("um"));
Statement s2 = Db.connectDb().createStatement();
ResultSet rs2 = s2.executeQuery("SELECT * FROM stock WHERE pid='" + rs1.getString("pid") + "'");
if (rs2.next()) {
v1.add(rs2.getString("qty"));
}
prodt.addRow(v1);
s2.close();
}
s1.close();
} catch (Exception e) {
e.printStackTrace();
}
}
将变量标记为protable
静态,或者将方法protable
非静态。
private static DefaultTableModel protable;
public static void refreshProtable() { ... }
Since the method is static, you call it using the class name that it is within. 由于该方法是静态的,因此可以使用其中的类名来调用它。
Eg 例如
class A {
public static void b() {
// do something
}
}
Would be called as follows: 将被称为如下:
A.b();
It might be handy to refresh yourself on how static variables work, here would be a starting point: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html 刷新一下静态变量的工作方式可能会很方便,这将是一个起点: http : //docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
you need to make protable
as static as you can access only static variables from a static method. 您需要使
protable
成为静态,因为您只能从静态方法访问静态变量。
private static DefaultTableModel protable;
public static void refreshProtable() { }
the variable you are trying to call is an instance-level variable; 您尝试调用的变量是实例级变量;
static variable 静态变量
It is a variable which belongs to the class and not to object(instance) 它是一个属于类而不属于对象(实例)的变量
Static variables are initialized only once , at the start of the execution . 静态变量在执行开始时仅初始化一次。 These variables will be initialized first, before the initialization of any instance variables
在初始化任何实例变量之前,将首先初始化这些变量
A single copy to be shared by all instances of the class 该类的所有实例共享一个副本
A static variable can be accessed directly by the class name and doesn't need any object 可以通过类名称直接访问静态变量,并且不需要任何对象
Syntax : . 句法 : 。
static method 静态方法
A static method can access only static data. 静态方法只能访问静态数据。 It can not access non-static data (instance variables)
它无法访问非静态数据(实例变量)
static method can call only other static methods and can not call a non-static method from it. 静态方法只能调用其他静态方法,而不能从中调用非静态方法。
A static method can be accessed directly by the class name and doesn't need any object 静态方法可以通过类名直接访问,不需要任何对象
Syntax : . 句法 : 。
A static method cannot refer to “this” or “super” keywords in anyway 静态方法无论如何都不能引用“ this”或“ super”关键字
You can make your function as non-static or make protable object static. 您可以使函数成为非静态对象,也可以使protable对象成为静态对象。
In a word, you can not reference non-static variable in static function. 总之,不能在静态函数中引用非静态变量。
but you can reference static variable in non-static function 但是您可以在非静态函数中引用静态变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.