Static import with wildcard - java

I was wondering if it is possible in Java to use static import with wildcards?
e.g.:
import static java.util.Arrays.*;

yes that is possible.
Static imports are for importing static members of classes. Just like regular imports, you can use a wildcard or import a specific member.
example
import static java.util.Arrays.asList; // importing static member asList
import static java.util.Arrays.* ; // importing all static members of Arrays class

You can. The import static will import all static members from the class. For example with Math:
import static java.lang.Math.*; // Imports all static members from Math
boolean isSmaller = E < PI;
If you didn't do this you would have to write:
boolean isSmaller = Math.E < Math.PI;
You should use static imports rarely though, as they tend to make your code harder to read. For a constants class it's okay to do it, if you use the constants a lot, but don't overuse it!

Related

importing a final class in Java

I have declared this class:
public final class Constants {
public static final String PROFILE_PCT__FILTER_NAME_ID = "profilePct";
}
I want to use that class in another class with..
import static com.tdk.utils.Constants.*;
But I got this compilation error:
Constants cannot be resolved to a variable
When you are using the static import with *, you have access to the variables defined in the class. However, you can't refer to the class itself.
Should work:-
import static com.tdk.utils.Constants.*;
String a =PROFILE_PCT__FILTER_NAME_ID;
Shouldn't work:-
String a =Constants.PROFILE_PCT__FILTER_NAME_ID;
You need the below import to resolve the compilation issue.
import com.tdk.utils.Constants;

How to import Java static method into Drools file?

The java class and static method code is:
public class DroolsStringUtils {
public static boolean isEmpty(String param) {
if (param == null || "".equals(param)) {
return true;
}
return false;
}
}
The drl code is:
package com.rules
import com.secbro.drools.utils.DroolsStringUtils.isEmpty;
rule CheckIsEmpty
when
isEmpty("");
then
System.out.println("the param is not empty");
end
But the IDEA hints:
"cannot relove" on the method 'isEmpty("")'.
I just want to import a static method from java class to drl file.
Use import static to import a static method.
import static com.secbro.drools.utils.DroolsStringUtils.isEmpty;
// ^^^^^^
(edited:) and of course you cannot call a static method where a pattern is required:
rule CheckIsEmpty
when
eval( isEmpty("") )
then
System.out.println("the param is not empty");
end
(It helps considerably to read the Drools documentation.)
[this might have details only relevant for Drools 6.0 & earlier (thanks Roddy of the Frozen Peas)]
Looks like you could import the static method, as a function into Drools...
import function com.secbro.drools.utils.DroolsStringUtils.isEmpty
...and then be able to use your rule as you wrote it originally in your Question.
It seems [classic Java-style] static imports are not supported in
Drools
But it seems they can be imported with Drools function import
style
FWIW: To reference static fields - import the class with the static fields as normal import (not static or function), and then refer to static field with static reference:
import your.package.with.static.field.YourClass
...
YourClass.STATIC_FIELD
(That also works for static methods too, if you don't want to import them as function).

Unable to do static import

//interface1.java
package package1;
public interface interface1 {
static final int a =10;
}
//StaticImportTest.java
import static package1.*; //import package1.*; works
class StaticImportTest {
public static void main(String args[]) {
System.out.println(a); //System.out.println(interface1.a) works
}
}
when i am replacing the word "import static" with only "import" and using System.out.println(interface1.a) it works, but not sure why its not working in its current form.
For your static import to work the way you intended it would have to be
import static package1.interface1.* or import static package1.interface1.a
A static import imports public static members of a class either all with * or a specific one like for example a.
A import on the other hand imports a package or specific classes from a package.
Your import static package1.* would try to import all members from the class package1 in the root package.
Making it a normal import and accessing a via interface1.a works because the import imports all classes from the package1 including interface1, therefor you can access a via the interface1 class.

Is there any way to reduce the length of static variable from another class?

I have a public class Helper in another file that has a few public static variables declared. I'm wondering if it's possible to shorten the variable name so that I don't have to consistently use Helper.<variable_name> everywhere. It would be ideal if I could just use the variable name without the Helper prefix.
Use
import static a.b.c.Helper.<variable_name>;
where a.b.c is the package which contains the Helper class.
You can use a static import:
// Class1.java
package test;
import static test.Class2.static_var;
public Class1 {
public static void main(String[] args) {
System.out.println(static_var); // not Class2.static_var
}
}
// Class2.java
package test;
public class Class2 {
public static String static_var = "Hello";
}
Output from java test.Class1:
Hello
Wildcard imports also work:
import static test.Class2.*;
Note that you must specify the full package name in the static import declaration. It is not sufficient to import test.Class2; and then import Class2.*;.

Importing packages in Java

How to import a method from a package into another program? I don't know how to import... I write a lil' code:
package Dan;
public class Vik
{
public void disp()
{
System.out.println("Heyya!");
}
}
and then, saved it in a folder named "Dan" and I compiled it. The .class file is generated. Then, I wrote this code below:
import Dan.Vik.disp;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
and I saved it outside the folder "Dan" and it says : "cannot find symbol"
I saved the first code in C:\Dan\Vik.java
and the second in C:\Kab.java
You don't import methods in Java, only types:
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
The exception is so-called "static imports", which let you import class (static) methods from other types.
In Java you can only import non-primitive types, or static methods/fields.
To import types use import full.package.name.of.TypeName;
//example
import java.util.List; //to import List interface
to import static methods/fields use
import static full.package.name.of.TypeName.staticMethod;
import static full.package.name.of.TypeName.staticField;
//example
import static java.lang.Math.max; //to import max method(s)
import static java.lang.Math.PI; //to import PI field
Take out the method name from in your import statement. e.g.
import Dan.Vik.disp;
becomes:
import Dan.Vik;
You should use
import Dan.Vik;
This makes the class visible and the its public methods available.
Here is the right way to do imports in Java.
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
You don't import methods in java. There is an advanced usage of static imports but basically you just import packages and classes.
If the function you are importing is a static function you can do a static import, but I don't think you are looking for static imports here.
In Java you can only import class Names, or static methods/fields.
To import class use
import full.package.name.of.SomeClass;
We can also import static methods/fields in Java and this is how to import
import static full.package.nameOfClass.staticMethod;
import static full.package.nameOfClass.staticField;
For the second class file, add "package Dan;" like the first one, so as to make sure they are in the same package; modify "import Dan.Vik.disp;" to be "import Dan.Vik;"

Categories