2

So I'm using Processing to draw a map from a data file. I want to stock some information of the first line inside a class. In processing this class is an inner class of PApplet, so it has to be a static class (Otherwise i get an error: static fieldscan only be declared in static or top level type).

public static class PlacesInfo{
    static final int totalCount;
    static final float xMin;
    static final float yMin;
    static final float xMax;
    static final float yMax;
    static final int populationMax;
    static final int densityMax;


    //Set all static variables once
    static{
      String[] lines =  loadStrings("population.tsv");
      String info = lines[0].substring(2); //to delete some useless char
      String[] infoInit = split(info, ','); //to parse the data of the first line

      totalCount = int(infoInit[0]);
      xMin = float(infoInit[1]);
      xMax = float(infoInit[2]);
      yMin = float(infoInit[3]);
      yMax = float(infoInit[4]);
      populationMax = int(infoInit[6]);
      densityMax = int(infoInit[8]); 
    }
}

When I run this code, I get an error, because I can't use the loadStrings() function (which is non-static).

So what I want is to have static final variables that I can initialize from the "population.tsv" file. What are your ideas/advices?

3
  • What the error is specifically? Commented Dec 1, 2011 at 23:44
  • 5
    Is there something preventing you from making loadStrings() a static method? Commented Dec 1, 2011 at 23:44
  • @Peter : My question was to consider going beyond this solution. Sorry if it wasn't clear. Commented Dec 8, 2011 at 11:34

3 Answers 3

2

1) Make the method static, and you will be fine -- static code must be in order must be compiled in order. Put the loadStrings function before the static code block.

Please note : However - you might be better off simply creating a single, static , "init" method, which is called in your static code block . This will be nameable and unit-testable, unlike your current implementation.

2) By the way : your float syntax is off, and must to be casted properly.

  int i = (int) 1.4f; 

3) To initialize the static variables you can do the following :

  • Declare a static init() method, which reads the file and initializes the static variables.
  • Create a separate FileReader object in a separate class, or a static inner class, which can be invoked to read in variables , and invoke it FROM your static code block.
  • (bad idea) Put the file reading logic inside of your big static code block . this will be very ugly however.
Sign up to request clarification or add additional context in comments.

2 Comments

I assumed from the question that loadStrings was not a static function. In which case order would not matter.
Thanks @jayunit100! That was the kind of things that I was trying to understand.
2

You cannot run class method loadString inside static context. In order to run it from this context, you need to make your loadString method static as well (or alternatively move it outside static context).

Comments

0

I am speculating:

String[] lines = ( new PApplet() ) . loadStrings("population.tsv");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.