-2

I am trying to import an enum from from another file. I have following code in the first file :

public class Colour {

    public enum Color {
        RED, ORANGE, YELLOW, GREEN, BLUE, BLACK, WHITE; // ; is optional

        @Override
        public String toString() {
            switch (this) {
            case RED:
                return "red";
            case ORANGE:
                return "orange";
            case YELLOW:
                return "yellow";
            case GREEN:
                return "green";
            case BLUE:
                return "blue";
            case BLACK:
                return "black";
            case WHITE:
                return "white";
            default:
                throw new IllegalStateException();
            }
        }

    }

Now I want to import these into another, lets say the class Test .

public class Test { }

How would I do that in both files? Thanks

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
RK.
  • 973
  • 5
  • 19
  • 45

5 Answers5

3

If they're in the same package, you don't have to import them - like any other java class.

If they're not, you'd do it like you'd import a class:

import com.mycompany.mypackage.Colour.Color;

EDIT: I just noticed it's a nested enum (sorry) - you still don't need to import it if it's in the same package, but to declare it you would use Colour.Color.

Note that you'd define the package in the Colour.java file like this:

package com.mycompany.mypackage;
Rob I
  • 5,627
  • 2
  • 21
  • 28
  • i am using notepad actually . – RK. Aug 03 '12 at 17:58
  • 1
    The editor you use doesn't make a difference - see my edits to see if they help clarify. – Rob I Aug 03 '12 at 18:00
  • so in first file i need to mention too the package name ? – RK. Aug 03 '12 at 18:02
  • If you don't declare what package a .java file belongs in, it will go into the "default package". It's not recommended for real software, but all classes in the default package can "see" each other without imports (just like other packages). If you _do_ want it in a package, declare it before the `class` line, like `package com.mycompany.package;`. – Rob I Aug 03 '12 at 18:04
  • i tried that one , now i am getting this error: 'error: expected import com.mycompany.package.Colour.Color;' – RK. Aug 03 '12 at 18:08
  • It's hard to see why without the whole code, but I am noticing now that "package" cannot be used as an identifier. Try something else (and I will edit my answer). – Rob I Aug 03 '12 at 18:09
1

This isn't an answer, but comments wouldn't let me format large blocks of code. I just wanted to propose some code improvements. To do exactly what your enum toString() is doing, you could shorten it significantly simply by using:

@Override
public String toString() { return this.name().toLowerCase(); }

Of course, if you want to be able to vary the values from the lowercase representation, then use a constructor to set the displayValue as such (much better than the case statement).

public class Colour {

    public enum Color {
        RED("red-ish"),
        ORANGE("orange-ish"),
        YELLOW("yellow-like"),
        GREEN("very green"),
        BLUE("like the sky"),
        BLACK("blanco"),
        WHITE("weiss");

        String displayVal;

        Color(String displaVal) { this.displayVal = displayVal; }

        public String toString() { return displayVal; }
    }

}
Kevin Welker
  • 7,719
  • 1
  • 40
  • 56
0

Assuming both enum & class are in the same package just declare as, say:

Colour myColour = Colour.RED;

Also would sugest to use Eclipse, it sorts out imports for you with shortcut key (CTRL + Shift + M).

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

If they are in the same package, you would not need to import them at all. If they are in different packages, use a java import statement. Or see this discussion for more information.

Community
  • 1
  • 1
Saish
  • 1,518
  • 1
  • 12
  • 21
0

Stop using Notepad and switch to an Integrated Development Environment (IDE) like Eclipse. You can download it for free and it helps you with imports, compiling, code formatting, auto-completion and a lot of other stuff. It may take a while to learn about all the features but it will immediately improve your life as a developer.

It seems you don't know about packages in Java. See this answer where I explain about packages.

Community
  • 1
  • 1
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • i use the IDE i-e netbeans but this is my assignment . i have to do with notepad otherwise this file is running in my netbeabs IDE but unable to do with notepad – RK. Aug 04 '12 at 05:05
  • If this is homework, please tag the question as such. – Adriaan Koster Aug 05 '12 at 08:19