Java static reference vs dynamic reference and calls

856 views Asked by At

Here I will post two simplified versions of my code (but it is self sufficient, you don't need to know what are other variables and so on).

I need the class including my problem to import my class Synchronous_Writing from another package. I need this class to be static as various classes must call it and modify the object created at first subsequently, and it is not really an option to pass the data from one class to another as that would make the code really unreadable...

This class is public, but still access from the current class involve some problem described below the version below. Thanks by advance for your help.

Backbone of Synchronous_Writing:

public final Synchronous_Writing {

     public Synchronous_Writing() {
     // variable to initialize
     }
     public static void writer ()
     {
     //operation to be performed
     }
 }

1st version:

import Synchronous_Writing;

public classFileToEdit_Parser {
  // A public static final class
  Synchronous_Writing SyncedWriter ; // note to an editor; this is a ";", not a {

  public FileToEdit_Parser  (
    String inputFile_FileToEdit, 
    String outputFile_EditedFile, 
    Charset charset, 
    String HitList_fieldsSeparator, 
    String CommentLine) 
    throws IOException, FileNotFoundException
  {
      SyncedWriter = new Synchronous_Writing(
      outputFile_EditedFile, charset, StandardOpenOption.APPEND);
  }

  protected void parser_writer(Path FileToEdit) 
    throws IOException, FileNotFoundException 
  {
    SyncedWriter.writer();
  }
}

//The problem here is that the compiler says that "the static method writer(String) from the type Synchronous_Writing should be accessed in a static way" (logical since Synchronous_Writing is static)

2nd version:

import Synchronous_Writing;
// A public static final class
public classFileToEdit_Parser
{
  public FileToEdit_Parser (
    String inputFile_FileToEdit, 
    String outputFile_EditedFile, 
    Charset charset, 
    String HitList_fieldsSeparator, 
    String CommentLine) throws IOException, FileNotFoundException
  {
    SyncedWriter = new Synchronous_Writing(
      outputFile_EditedFile, charset, StandardOpenOption.APPEND);
  }

  protected void parser_writer(Path FileToEdit) 
    throws IOException, FileNotFoundException 
  {
  // SyncedWriter.writer () 
  // won't work, I don't really understand why since 
  // the constructor is public, but I still can't access it

  Synchronous_Writing.writer () 
  // will work, but the compiler keeps saying that SyncedWriter
  // variable is not used (which is quite true, since only the
  // constructor is initialized)
  }
}

To correct 1st version's problem, I thus suppressed Synchronous_Writing SyncedWriter; in class declaration and switched to a variable call through the class and not the object. Suppressing Synchronous_Writing SyncedWriter; was not mandatory, still, but it doesn't change the problem.

My second problem is that SyncedWriter.writer () won't work, I don't really understand why since the constructor is public, but I still can't access it. Synchronous_Writing.writer () will work, but the compiler keeps saying that SyncedWriter variable is not used (which is quite true, since only the constructor is initialized)

In the end, both codes are botched in some way, and I would like to know it properly, so please feel free to give your insights.

2

There are 2 answers

9
mrak On

static in java means, that a method do not know about any context except passed parameters and other static variables.

You are trying to create a context in Synchronous_Writing and than call a static method on this context. Calling the method without the step to initialize the context in Synchronous_Writing will probably case an exception. This is a bad practice and not self-explanatory code.

If you need a static method, make sure it doesn't depend on any context which needs to be initialized.

1
Roel Strolenberg On

In general, when you want to modify an instance (object) of a class from multiple other classes, use the Singleton design pattern. You only need to have a static getter and all your problems will be solved. Google Singleton design pattern java example and be convinced of it's awesomeness and applicability!