2

I am quite new to using packages in Java and I would like to know if there is an easier way to import classes with fewer import statements.

I am using Processing and I have started using Box2D for Processing to create some games.

In order to use the library, I have to add the following to my sketch:

import shiffman.box2d.*;

import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.joints.*;

To simplify the imports, I have tried:

import shiffman.box2d.*;

import org.jbox2d.*;

However, when I define a body using BodyDef, I get an error saying that BodyDef is not defined.

Processing error

It seems that the error is also mentioning that org.jbox2d.* is not a package...

Is there a way to have fewer import statements? PDE doesn't seem to include those as well...

Thanks in advance!

Ed The ''Pro''
  • 875
  • 10
  • 22
  • 1
    Could you put screenshot or whole description of this error ? – Rafał Sokalski Apr 17 '19 at 09:18
  • 1
    Why are you worrying about the imports? Your IDE should sort that for you – Jay Apr 17 '19 at 09:18
  • 1
    There is no concept of a subpackage in Java (see duplicate). `org.jbox2d.dynamics` and `org.jbox2d.dynamics.joints` are completely distinct entities. They have similar names but the hierarchy that we sometimes view in our heads does not actually exist in practice. As there is no relationship between these packages, there is no way to accomplish this. – Michael Apr 17 '19 at 09:30
  • @Jay PDE does not seem to have the capabilities to reference them. Also, it was mentioned in the book Nature of Code that I have to manually import the package. – Ed The ''Pro'' Apr 17 '19 at 13:16
  • @RafałSokalski I added an image – Ed The ''Pro'' Apr 17 '19 at 13:16
  • @Michael I rephrased the question a bit. The problem that I'm having trouble with is that it can be a bit hard to remember what I need to import. I was wondering if there was a simpler way... – Ed The ''Pro'' Apr 17 '19 at 13:19
  • Where is your class definition? Method and field cannot be outside class – Rafał Sokalski Apr 17 '19 at 14:37

1 Answers1

2

You can only import Types and static methods in Java.

A similar question is actually answered in: Importing packages in Java.

George
  • 58
  • 7