-2

Related to

Cannot make a static reference to the non-static method

Context

In Processing, I use the method loadImage() inside a Inner Class. Processing disagrees :

Cannot make a static reference to a non-static method loadImage(String) from the type PApplet

If I make the main class non-static, then, the error message disappears. I don't understand why my main class can't be static, I don't want to instantiate it because I don't need it.

The inner class is used to create an array of objects.

Code

class PApplet
{
    public loadImage(foo)
    {
        // ...
    }
}

static class MainStaticClass
{
    class InnerClass
    {
        public nonStaticMethod(foo)
        {
            return PApplet.loadImage(foo); // Error 
        }
    }
}
snoob dogg
  • 2,491
  • 3
  • 31
  • 54

2 Answers2

0

In the future, please try to post a MCVE. The code you posted now is not valid syntax and doesn't really reflect what you're actually doing.

Anyway, the loadImage() function is not static, so you can't do this:

PApplet.loadImage("img.jpg");

And it's also worth noting that every sketch extends the PApplet class, so this:

PImage img;

void setup(){
  img = loadImage("img.jpg");
}

Is converted to Java code that looks like this:

public class MySketch extends PApplet{
  PImage img;

  void setup(){
    img = loadImage("img.jpg");
  }
}

Here, we can see that the loadImage() function is coming from the PApplet class, which we're extending.

So now you have something like this:

public class MySketch extends PApplet{
  PImage img;

  void setup(){
    img = MyInnerClass.getImage("img.jpg");
  }

  class MyInnerClass{
    public static PImage getImage(String loc){
      return loadImage(loc);
    }

  }
}

Or something like this:

public class MySketch extends PApplet{
  PImage img;

  void setup(){
    img = new MyInnerClass().getImage("img.jpg");
  }

  static class MyInnerClass{
    public PImage getImage(String loc){
      return loadImage(loc);
    }

  }
}

In either case, this will generate an error saying that you can't use the non-static loadImage() function from a static context.

That's because the loadImage() function is coming from the instance itself. You need an instance to use a non-static function. But since the getImage() function is static, it doesn't belong to any particular instance. That's why it doesn't work.

If I were you, I would take a step back and ask yourself why you need inner classes at all. If it's a static function, then why not just use a sketch-level function?

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thank you, I must admit that my question is really bad written, not always easy to figure out what must be added. I solved my problem simply by making my main class non-static and by instantiate it as you recommend. With the (bad) understanding I've of OOP , It sometimes hard to distinguish situations where you don't need instance at all than when you need just one. I forgot to mention that all of this class is in a second tab from my processing project, it's not representing a sketch. So all is encapsulated in a main class to make it easier to import in a new project. – snoob dogg Sep 20 '17 at 19:52
  • @snoobdogg New tabs are still converted into inner classes, unless the tab ends with `.java`. – Kevin Workman Sep 20 '17 at 20:03
-1

Top Level Classes are Static so its redundant and confusing see Specification Inner classes are by default non static , you can declare them as Static. For a non static method you have to instantiate an object of class else things come toppling down - :-)

Community
  • 1
  • 1