What Is Static Import Inwards Coffee V Alongside Example
Sunday, July 8, 2018
Add Comment
Static import inward Java allows to import static members of shape in addition to travel them, every bit they are declared inward the same class. Static import is introduced inward Java v along alongside other features similar Generics, Enum, Autoboxing in addition to Unboxing in addition to variable declaration methods. Many programmer intend that using static import tin move reduce code size in addition to allow yous to freely travel static champaign of external shape without prefixing shape cite on that. For illustration without static import yous volition access static constant MAX_VALUE of Integer shape every bit Integer.MAX_VALUE but yesteryear using static import yous tin move import Integer.MAX_VALUE in addition to refer it every bit MAX_VALUE. Similar to regular import statements, static import likewise allows wildcard * to import all static members of a class. In side yesteryear side department nosotros volition encounter Java programme to demonstrate How to travel static import statements to import static fields.
Static import illustration inward Java
In this static import example, nosotros accept imported constants Integer.MAX_VALUE in addition to Integer.MIN_VALUE statically in addition to printing at that topographic point value without prefixing class name on that.
package test;
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.MIN_VALUE;
/**
*
* Java programme to demonstrate How to travel static import inward Java 5
* By using static import yous tin move travel static champaign of external class
* every bit they are declared inward same class.
*
* @author Javin Paul
*/
public class StaticImportExample {
public static void main(String args[]) {
//without Static import
System.out.println("Maximum value of int variable inward Java without " +
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.MIN_VALUE;
/**
*
* Java programme to demonstrate How to travel static import inward Java 5
* By using static import yous tin move travel static champaign of external class
* every bit they are declared inward same class.
*
* @author Javin Paul
*/
public class StaticImportExample {
public static void main(String args[]) {
//without Static import
System.out.println("Maximum value of int variable inward Java without " +
"static import : " + Integer.MAX_VALUE);
System.out.println("Minimum value of int variable inward Java without " +
System.out.println("Minimum value of int variable inward Java without " +
static import : " + Integer.MIN_VALUE);
//after static import inward Java 5
System.out.println("Maximum value of int variable using " +
//after static import inward Java 5
System.out.println("Maximum value of int variable using " +
static import : " + MAX_VALUE);
System.out.println("Minimum value of int variable using" +
System.out.println("Minimum value of int variable using" +
static import : " + MIN_VALUE);
}
}
Output:
Maximum value of int variable inward Java without static import : 2147483647
Minimum value of int variable inward Java without static import : -2147483648
Maximum value of int variable using static import : 2147483647
Minimum value of int variable using static import : -2147483648
}
}
Output:
Maximum value of int variable inward Java without static import : 2147483647
Minimum value of int variable inward Java without static import : -2147483648
Maximum value of int variable using static import : 2147483647
Minimum value of int variable using static import : -2147483648
If yous human face at import statements import static java.lang.Integer.MAX_VALUE, its written every bit import static rather than static import, thence simply beware of that. We are non using * wildcard hither in addition to importing entirely selected static fellow member but yous tin move likewise travel import static java.lang.Integer.* to import all static fields inward 1 go.
Advantages of Static Import inward Java
Main payoff of using static import inward Java is saving keystrokes. If yous are often using System.out.println() statements in addition to tried of typing it, yous tin move static import System.out or System.* in addition to afterwards yous tin move type out.println() inward your code, Though I would advise to travel this Eclipse shortcut to generate System.out.println statement which is much faster than static import. This is the form of usage I encounter 1 tin move create goodness from static import, other than that static import is simply extension of regular import declaration inward Java. Similar to static champaign yous tin move likewise import static method inward your class, by in addition to large inward instance of Utility classes.
Drawback of Static Import inward Java
Many Java programmer fence against static import alongside argue that it reduces readability in addition to goes against how static champaign should endure used i.e. prefixed alongside shape cite e.g. Integer.MAX_VALUE. Static import has about other drawback inward damage of conflicts, in 1 lawsuit yous static import Integer.MAX_VALUE yous tin move non travel MAX_VALUE every bit variable inward your programmer, compiler volition throw error. Similarly if yous static import both Integer.MAX_VALUE in addition to Long.MAX_VALUE in addition to refer them inward code every bit MAX_VALUE, yous volition larn next compile time mistake :
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable beginning code - MAX_VALUE is already defined inward a static single-type import
at test.StaticImportExample.(StaticImportExample.java:6)
Could non detect the psyche class: test.StaticImportExample. Program volition exit.
Exception inward thread "main" Java Re
Summary
Finally few points worth remembering virtually static import inward Java :
1) Static import statements are written every bit "import static" inward code in addition to non "static import".
2) If yous import 2 static fields alongside same cite explicitly e.g. Integer.MAX_VALUE in addition to Long.MAX_VALUE in addition to thence Java volition throw compile fourth dimension error. But if other static modifier is non imported explicitly e.g. yous accept imported java.lang.Long.*, MAX_VALUE volition refer to Integer.MAX_VALUE.
3) Static import doesn't amend readability every bit expected, every bit many Java programmer prefer Integer.MAX_VALUE which is clear that which MAX_VALUE are yous referring.
4) You tin move apply static import statement non entirely on static fields but likewise on static methods inward Java.
That's all on What is static import inward Java 5, What is advantages in addition to drawbacks of using static import inward Java programme in addition to how to travel static import inward Java. Honestly, its been almost a decade alongside Java v released but I accept rarely used static import statements. May endure inward hereafter I may figure out a rigid convincing argue to travel static import
Further Learning
Complete Java Masterclass
What is departure betwixt bounded in addition to unbounded wildcards inward Generics?
0 Response to "What Is Static Import Inwards Coffee V Alongside Example"
Post a Comment