0

I have this import source :

import static   MinecraftDungeonTileTypes.*;

on class in same package as the class MinecraftDungeonTileTypes

source of class:

package mod.dungeonworld;

public  class MinecraftDungeonTileTypes {
    public static  int TILE_WALL = 0;
    public static  int TILE_ROAD = 1;
    public static  int TILE_DOOR = 2;
    public static  int TILE_CHEST = 3;
    public static  int TILE_ROOM = 4;
    public static  int TILE_COD = 5;
    public static  int TILE_SPAWNER = 6;
}

What I get Is an compilation error:

Description Resource Path Location Type The import MinecraftDungeonTileTypes cannot be resolved.

When I use the static members of class MinecraftDungeonTileTypes regularly without static import I get no compile error. What is causing that?

2 Answers 2

2

You can't statically import a non static class but you can import statically it's static fields / attributes:

In order to access static members, it is necessary to qualify references with the class they came from. For example, one must say:

Import single attribute (use * instead of name to import massively)

import static mod.dungeonworld.MinecraftDungeonTileTypes.TILE_WALL;

You will refer in the code as TILE_WALL.

Or import the class

import mod.dungeonworld.MinecraftDungeonTileTypes;

You will refern to same attribute in the code as MinecraftDungeonTileTypes.TILE_WALL.

Sign up to request clarification or add additional context in comments.

1 Comment

"You can't statically import a non static class but you can import statically it's static fields / attributes" Nice info, but since OP doesn't do that, it is not really related to this question.
2

The problem comes from that I haven't written the full path of the class I want to import. Here is solution in my case:

import static   mod.dungeonworld.MinecraftDungeonTileTypes.*;

2 Comments

@Hiru Why should he? Do you know what this page is for?
@Hiru What!? There are plenty of great self-answers on this website. In fact, it's actively encouraged to answer your own question when you find the answer!

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.