Nayana Rajapaksha
Nayana Rajapaksha

Reputation: 161

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(); . the error is non-static variable protable cannot be referenced from a static context.

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();
    }
}

Upvotes: 0

Views: 109

Answers (4)

Rahul
Rahul

Reputation: 16355

you need to make protable as static as you can access only static variables from a static method.

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

  • It is a method which belongs to the class and not to the object(instance)
  • 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

Upvotes: 0

zwang
zwang

Reputation: 705

You can make your function as non-static or make protable object static.

In a word, you can not reference non-static variable in static function.

but you can reference static variable in non-static function

Upvotes: 0

Gary
Gary

Reputation: 916

Since the method is static, you call it using the class name that it is within.

E.g

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

Upvotes: 0

Srinivas
Srinivas

Reputation: 1790

Either mark your variable protable static or make the method non-static.

private static DefaultTableModel protable;

public static void refreshProtable() { ... }

Upvotes: 3

Related Questions