1

I am kind of new to Java and I am trying some stuff with swing.

    class CustomListSelectionListener implements ListSelectionListener {
    public void valueChanged (ListSelectionEvent e)
    {
        //All this listener does is to return the NAME of the selected connection to the static method gotocategories.
        JList lsm = (JList)e.getSource();
        Home.goToCategories((String)lsm.getSelectedValue());
    }

problem is, the listener is a class, so basically if I call a static method from the "home class" I need to make all the variables I have to use in the "goToCategories" function static.

    public static void goToCategories(String collectionname)
{
    Statement stmt;
    try
    {
        stmt = privconn.createStatement();
        //Getting the collection ID;
        String firststmt = "SELECT * FROM collection WHERE Name='"+collectionname+"' AND User_ID = "+userID+"";
        ResultSet rs = stmt.executeQuery(firststmt);
        rs.next();
        int id = rs.getInt("ID");
        JOptionPane.showMessageDialog(null, id);

    } catch(Exception E)
    {
        E.printStackTrace();
    }
}

This code works , but I think I am creating a lot of static variables and I am not sure that's the best way to do it. Of course as soon as I try and remove the static it'll say "Cannot make static reference to non-static field ..."

arocketman
  • 1,134
  • 12
  • 21
  • 3
    You should read up and understand exactly what `static` means. http://stackoverflow.com/questions/3903537/i-want-to-know-the-difference-between-static-method-and-non-static-method and http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html are good references. – Mike B Jan 22 '14 at 21:06
  • 2
    Note: You don't want to [create SQL queries like that](http://en.wikipedia.org/wiki/SQL_injection). Use [prepared statements](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) instead. – Alexis C. Jan 22 '14 at 21:08
  • Or a library that handles all that stuff for you like Hibernate. – corsiKa Jan 22 '14 at 21:09

2 Answers2

1

If your CustomSelectionListener class has a reference to an instance of your Home class, you will be able to call non-static methods on that instance.

It would look something like this:

MyClass someObject = new MyClass();
someObject.nonStaticMethod();

However static methods are called without an instance of the class, by using the name of the class:

MyClass.someStaticMethod();

It is a design decision whether a method or member variable should be static, and as the comments say you should fully understand what static means in order to correctly make this decision.

user1445967
  • 1,520
  • 4
  • 14
  • 30
  • I do understand what you wrote, I chose the second option here. What I would like to understand is: In terms of memory usage, what is better? – arocketman Jan 22 '14 at 21:14
  • Instantiating objects does consume memory, in proportion to what is required to store the data of that objects members. – user1445967 Jan 22 '14 at 22:03
0

I think the best way to do that, is making a class without a main method,
do all your stuff in the constructer, and then make a new instance of that class from a main method in an other class(new my class),

java-love
  • 516
  • 1
  • 8
  • 23