Search code examples
javaclassmethodspackaging

How to access methods in separate classes


I am trying to access a public method in a separate class but for some reason Netbeans cannot find it. They are in the same project and packaged together. The error message says it is looking for it in the class I am calling it from (Project6). Any ideas on how can I get it to look in the right class (HashTable)?

class HashTable {

    //.....

    public HashTable(int size) {
        arraySize = size;
    }
}

public class Project6 implements ActionListener {

    //.....

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == hashButton) {
            text = hashSizeField.getText();
            HashTable(Integer.parseInt(text)); //error occurs here
        }
    }
}

Solution

  • The method

    public HashTable(int size)
    

    is a constructor of the class HashTable, you need call with the key word new . The code will be:

    class HashTable {
    
    
      public HashTable(int size) {
         arraySize = size;
      }
    }
    
    public class Project6 implements ActionListener {
    
    //.....
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == hashButton) {
            text = hashSizeField.getText();
            new HashTable(Integer.parseInt(text)); //error occurs here
        }
    }
    

    }