Search code examples
javastatic-methodsstatic-classstatic

Is there any way to use a non-static method in a static class?


I'm trying to implement a simple spreadsheet using Java. It interacts with the user through some menus on the console, and a user can import a given file with some pre-made info about the spreadsheet to be created (lines, columns, and content of cells). I am trying to make a static class which I called Parser, and the goal of this class is to break down each line of the import into little pieces so I can then apply the correct methods to them (read the cell to which content is being added, and what type of content am I trying to add).

I made my Parser static, because I want to use it without the need of instantiating a new object every time I need it (is this correct?). I have a specific method that is giving me trouble though. Whenever I receive input like this: 1;1|=2;3it means that my cell 1;1 references the cell 2;3. I am telling the parser to return a new Reference(getCell(i,j)). This is because my Reference class constructor receives a Cell, but of course the java compiler tells me I cannot use a non-static method, which is the case of the getCell, inside that static class.

So my question is: is there any way to overcome this problem? Any way to use a non-static method in a static class or should I instantiate a new Parser object when I try to read an import file?


Solution

  • It might be helpful to see some of your code to determine which method is more appropriate, but as this is a common pitfall in object oriented design, I'll try to answer the question generically.

    If you define something as static, that means it has no association with any instances, even though it shares the class name. For instance,

    public class Table {
      List<Cell> cells;
    
      public Table() {
        while (someCondition)
          parseInput(nextInput);
      }
    
      public Cell getCell(int i, int j) {
        ...
      }
    
      public static Cell parseInput(String input) {
        Cell cellToReturn = new Cell();
        ...
        if (input.references(cell)) cell = getCell(i,j); //Error here!
        ...
        return cellToReturn;
      }
    }
    

    The problem arises because the parseInput method is static, and yet it is referencing a specific instance's list of cells! Which one is it referencing? Who knows!

    You can solve the issue two ways:

    1: Make the parser non-static: public Cell parseInput(String input) {

    2: Pass the table to the parser, so it knows what to reference:

    public static Cell parseInput(String input, Table target) {
      Cell cellToReturn = new Cell();
      ...
      if (input.references(cell)) cell = target.getCell(i,j); //No more error!
      ...
      return cellToReturn;
    }
    

    Now, as you stated, the parser is a class, not just a method. But the general rule still applies. You cannot reference an instance field from a static method, because that static method is not associated with any instance!