Static import

Static import importing all the static members i.e., static variables and static methods in another packages. So that they can be used by their simple name.

For example:
package com.gk.doc public interface Constants { String NAME = "Geek"; String WEBSITE = ""; } //In the above example we take two constant fields i.e, NAME, WEBSITE. //In interface by default we get public ,final, static no need to declare. package com.gk.doc; import documentation.Constants; public class RGM { public void genReport() { String x = Constants.NAME; String y = Constants.WEBSITE; } }

In this example we are importing Constant class in another package so we can directly access that fields by using class name without creating instance.

import static com.gk.Constants.NAME; import static com.gk.Constants.WEBSITE; public class RGM1 { public void genReport1(){ String x = NAME; String y = WEBSITE; } }

In this we import Constant class variables directly using static keyword in import statement we can call these variables directly no need of class name & no need of creating an instance for that Constant class.

HTML Comment Box is loading comments...