5

I have 2 classed at different pages.

The object class:

public class Sensor {

  Type type;
  public static enum Type
  {
        PROX,SONAR,INF,CAMERA,TEMP;
  }

  public Sensor(Type type)
  {
  this.type=type;
  }

  public void TellIt()
  {
      switch(type)
      {
      case PROX: 
          System.out.println("The type of sensor is Proximity");
          break;
      case SONAR: 
          System.out.println("The type of sensor is Sonar");
          break;
      case INF: 
          System.out.println("The type of sensor is Infrared");
          break;
      case CAMERA: 
          System.out.println("The type of sensor is Camera");
          break;
      case TEMP: 
          System.out.println("The type of sensor is Temperature");
          break;
      }
  }

  public static void main(String[] args)
    {
        Sensor sun=new Sensor(Type.CAMERA);
        sun.TellIt();
    }
    }

Main class:

import Sensor.Type;

public class MainClass {

public static void main(String[] args)
{
    Sensor sun=new Sensor(Type.SONAR);
    sun.TellIt();
}

Errors are two, one is Type can not be resolved other is cant not import. What can i do? I first time used enums but you see.

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
CursedChico
  • 571
  • 4
  • 7
  • 17
  • See Reimeus's answer which should be accepted (the class containing the `enum` cannot be in the default package). Or [look here](http://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package). – mins Oct 07 '14 at 06:08

3 Answers3

10

enums are required to be declared in a package for import statements to work, i.e. importing enums from classes in package-private (default package) classes is not possible. Move the enum to a package

import static my.package.Sensor.Type;
...
Sensor sun = new Sensor(Type.SONAR);

Alternatively you can use the fully qualified enum

Sensor sun = new Sensor(Sensor.Type.SONAR);

without the import statement

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

For static way give proper package structure in the static import statement

import static org.test.util.Sensor.Type;
import org.test.util.Sensor;
public class MainClass {
    public static void main(String[] args) {
        Sensor sun = new Sensor(Type.SONAR);
        sun.TellIt();
    }
}
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
Veera
  • 1,775
  • 2
  • 15
  • 19
2

The static keyword has no effect on enum. Either use the outer class reference or create the enum in its own file.