Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Java enum switch case and namespace import works

Tags:

java

I am a little confused about the Java import system (I am not sure it is about Java import or this library - I use Apache POI 3.17):

import java.io.FileInputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Main {
    public static void main(String[] args) {
        try {
            Workbook wb = WorkbookFactory.create(new FileInputStream(
                    "workbook.xls"));
            Sheet sheet1 = wb.getSheetAt(0);
            for (Row row : sheet1) {
                for (Cell cell : row) {
                    // Alternatively, get the value and format it yourself
                    switch (cell.getCellTypeEnum()) {
                    ///////////////// Why this is not CellType.STRING
                    case STRING:
                        System.out.println(cell.getRichStringCellValue()
                                .getString());
                        break;
                    case NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell)) {
                            System.out.println(cell.getDateCellValue());
                        } else {
                            System.out.println(cell.getNumericCellValue());
                        }
                        break;
                    case BOOLEAN:
                        System.out.println(cell.getBooleanCellValue());
                        break;
                    case FORMULA:
                        System.out.println(cell.getCellFormula());
                        break;
                    case BLANK:
                        System.out.println();
                        break;
                    default:
                        System.out.println();
                    }
                }
            }

        } catch (Exception e) {

        }
    }

}

From the import section, you can see I did not import:

import org.apache.poi.ss.usermodel.CellType;

Why does this code work in that switch section? In each case I only use an enum type there (like STRING but not CellType.STRING). How does the Java compiler know where those types come from? I edit the code in Eclipse and when I hover over that "case STRING", it shows org.apache.poi.ss.usermodel.CellType.STRING?

like image 556
Kuan Avatar asked Mar 02 '18 22:03

Kuan


1 Answers

The JLS, Section 14.11, says:

If the type of the switch statement's Expression is an enum type, then every case constant associated with the switch statement must be an enum constant of that type.

The compiler only requires a simple name, because it already knows what type it needs to be.

SwitchLabel:

    case ConstantExpression :

    case EnumConstantName :

    default :

EnumConstantName:

    Identifier

And an Identifier is a simple name without ..

But this is a different concept from importing packages. Importing isn't the mechanism that allows a type to be used; it's not like not importing a package disallows the use of that package. After all, one can always use the fully qualified name of a type without importing it, e.g.

org.apache.poi.ss.usermodel.CellType cellType = cell.getCellTypeEnum();

Importing packages is a convenience for us as programmers so that we don't need to type the fully qualified type name every time we need to reference the type; we just need to type the simple name.

The JLS, Section 7.5, states:

An import declaration allows a named type or a static member to be referred to by a simple name (§6.2) that consists of a single identifier.

Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name (§6.7).

Importing a package doesn't influence whether the compiler knows about a package or a class within the package; it only allows programmers to use the simple name for convenience.

like image 165
rgettman Avatar answered Sep 27 '22 20:09

rgettman