Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import constant fields

Tags:

java

I want to import my constants which are in package consts and in class Constants. There I have inside classes.

For example I want to use form Capability.DEVICE_NAME instead of Constants.Capability.DEVICE_NAME

public class Constants {
    public class Capability{
        public final static String DEVICE_NAME = "deviceName";
        public final static String PLATFORM_NAME = "platformName";
        public final static String PLATFORM_VERSION = "platformVersion";
        public final static String APP_PACKAGE = "appPackage";
        public final static String APP_ACTIVITY = "appActivity";
    }
}

It have to be in inside classes!

Thank in advance.

like image 970
k.szulc Avatar asked Oct 27 '25 18:10

k.szulc


2 Answers

You will have to import your class like below

import packageName.Constants.Capability;

You can use as you want like below:

System.out.println(Capability.DEVICE_NAME);  

Or You can make your class static and import as below.

import static com.Constants.*;
like image 193
Amit Garg Avatar answered Oct 30 '25 08:10

Amit Garg


This import packagename.Constants.*; should work.

Or you can make the nested class static

public class Constants {
    public static class Capability{
        public final static String DEVICE_NAME = "deviceName";
        public final static String PLATFORM_NAME = "platformName";
        public final static String PLATFORM_VERSION = "platformVersion";
        public final static String APP_PACKAGE = "appPackage";
        public final static String APP_ACTIVITY = "appActivity";
    }
}

and import static:

import static packagename.Constants.*;
like image 41
Liviu Stirb Avatar answered Oct 30 '25 07:10

Liviu Stirb