When creating a class, what is it an inner class of? "declared static in a non-static inner type"

I need to use a static method/variable in a non static class, but because it is an inner class I can't. Is there a way around this? If I make my classes in .java files they are top level classes but then I have to go through the hassle of importing any libs I need access to and it becomes increasingly difficult to work with the processing draw functions and access global variables created in the main tab of the sketch. I understand the uselessness of having a static method in a non-static inner class but I don't even know what the outer class is or what it's doing.

Example Class

class Example{

  public static boolean iNeedHelp = true;

  Example(){
    print("The class needs to be non-static");
  }

}

Attempting to compile and run this code just throws "The field iNeedHelp cannot be declared static in a non-static inner type, unless initialized with a constant expression"

EDIT: Found some info that I think partially answers my question here, but how would I go about passing an instance of the main class into one of my .java files? https://processing.org/discourse/beta/num_1233971698.html

Answers

  • Answer ✓

    how would I go about passing an instance of the main class into one of my .java files?

    through the constructor.

    see this https://processing.org/tutorials/eclipse/ specifically the "Processing in Eclipse with Multiple Classes" bit

  • edited July 2017 Answer ✓

    There are some approaches for it. For example, you may simply declare your nested class as static:
    static class Example {}

    Or you may create another static class and extends it in your actual class: :ar!

    /** 
     * Static Class Extends (v1.0)
     * GoToLoop (2017/Jul/29)
     *
     * Forum.Processing.org/two/discussion/23623/
     * when-creating-a-class-what-is-it-an-inner-class-of-
     * declared-static-in-a-non-static-inner-type#Item_2
     */
    
    Example example;
    
    void setup() {
      example = new Example();
      println(example.iNeedHelp); // true
    
      Example.iNeedHelp = false;
      println(example.iNeedHelp); // false
    
      exit();
    }
    
    static abstract class ExampleStatic {
      static boolean iNeedHelp = true;
    }
    
    class Example extends ExampleStatic {
    }
    
  • edited July 2017 Answer ✓

    If you can't either use extends or prefer 1 class only, but can't declare it static b/c you wanna access Processing's non-static API members inside your class, you're gonna need to request the sketch's PApplet reference in its constructor and store it in some field, so you can call Processing's methods & access its fields there too: #:-S

    /** 
     * Static Class Non-Extends (v1.0.1)
     * GoToLoop (2017/Jul/29)
     *
     * Forum.Processing.org/two/discussion/23623/
     * when-creating-a-class-what-is-it-an-inner-class-of-
     * declared-static-in-a-non-static-inner-type#Item_3
     */
    
    Example2 example;
    
    void setup() {
      size(800, 600);
      smooth(3);
      noLoop();
    
      colorMode(RGB);
      blendMode(REPLACE);
      ellipseMode(CENTER);
    
      strokeWeight(Example2.BOLD);
      stroke(Example2.STROKE);
    
      example = new Example2(this);
      println(example.iNeedHelp); // true
      println(Example2.iNeedHelp); // true
    }
    
    void draw() {
      background((color) random(#000000));
      example.display();
    }
    
    void mousePressed() {
      redraw();
    }
    
    static class Example2 {
      static final color STROKE = #008000, FILL = #FFFF00;
      static final float BOLD = 3.5;
    
      static boolean iNeedHelp = true;
      final PApplet p;
    
      Example2(final PApplet pa) {
        p = pa;
      }
    
      void display() {
        p.fill(FILL);
        p.ellipse(p.width>>1, p.height>>1, p.width>>1, p.height>>1);
      }
    }
    
  • I didn't make the connection that because the sketch gets compiled into a single class (aside from any included .java files) you can use 'this' to pass the PApplet into another class. I'm also still not entirely sure how the PApplet works but I understand it enough to get my sketch working again. Thank you for the speedy replys!

  • edited July 2017
    • All ".pde" tab files are concatenated together as 1 class which extends PApplet. L-)
    • Therefore, all our classes are nested to that big wrapper class and are free to access all Processing's API w/o any explicit reference. :-bd
    • However, if we declare a nested class as static, it behaves pretty much like any regular top class from ".java" tab files. :ar!
  • That makes sense. Thank you!

  • edited July 2017

    Ummmm... cannot find a class or type named "PApplet"

    EDIT: You need to import it!

    import processing.core.PApplet;
    
  • Another thing to note is that this doesn't give the class access to global variables you create, only the processing static functions (as far as I can tell)

  • edited July 2017

    My 2 examples are supposed to just copy & paste & run on the PDE (Processing's IDE).

    If you end up needing to import PApplet, then it's a ".java" file rather than a ".pde" 1. :-@

  • As a hint, try exporting a sketch and looking in the exported directory for the Java file that gets created, you'll see what the preprocessor does and that might help you get at the global variables (which i think will just become member variables of the outer class, but i haven't tested that and am not sure it'll help)

  • I don't think you will have access to global variables from a static context or from a java class. I mean, those global variables in the main tab (or visible to all other pde tabs) are encapsulated in the main PApplet instance. Your class cannot access them directly but you can provide methods to retrieve this data or keep them updated. Without knowing about your final requirment, I can only provide one approach using @GoToLoop's prev post.

    Below, myNumber is what we know as a global variable.

    Kf

    Example2 example;
    int myNumber=1001;
    
    void setup() {
      size(800, 600);
      smooth(3);
      noLoop();
      textSize(60);
    
      colorMode(RGB);
      blendMode(REPLACE);
      ellipseMode(CENTER);
    
      strokeWeight(Example2.BOLD);
      stroke(Example2.STROKE);
    
      example = new Example2(this,myNumber);
      println(example.iNeedHelp); // true
      println(Example2.iNeedHelp); // true
    }
    
    void draw() {
      background((color) random(#000000));
      example.display();
    }
    
    void mousePressed() {
      redraw();
      changeNumber();
    
    }
    
    void changeNumber(){
     myNumber=(int)random(frameCount); 
     Example2.currentNumber=myNumber;
    }
    
    
    
    static class Example2 {
      static final color STROKE = #008000, FILL = #FFFF00;
      static final float BOLD = 3.5;
    
      static boolean iNeedHelp = true;
      static int currentNumber=0;
      final PApplet p;
    
      Example2(final PApplet pa, int n) {
        p = pa;
        currentNumber=n;
      }
    
      void display() {
        p.fill(FILL);
        p.ellipse(p.width>>1, p.height>>1, p.width>>1, p.height>>1);
        p.fill(0);
        p.text("NOW is "+currentNumber,p.width*0.3,p.height/2);
      }
    }
    
  • I just made a static field for each of the ones my class needed and created a static init method to call once they were set. They don't change so it worked pretty well.

    import processing.core.PApplet;
    import java.util.ArrayList;
    
    public class Tron {
    
      private static Die die = new Die(4);
      private static PApplet p;
      private static int arenaSize;
      private static float scale;
      private static float offset;
      private static int totalTrons = 0;
    
      private Pair pos;
      private ArrayList<Pair> tail;
      private int dir = die.roll() - 1;
      private boolean dead = false;
      private Score score;
      public int hue;
      public boolean newScore = false;
    
    //contructors
    
      public static void init(PApplet pa, int aSize, float scl, float offst) {
        p = pa;
        arenaSize = aSize;
        scale = scl;
        offset = offst;
      }
    
    //the rest of the class
    
    }
    
Sign In or Register to comment.