importing a final class in Java - 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;

Related

Static import with wildcard

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!

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.*;.

Why is import statement not needed in this instance?

Why does Java allow me to exclude the import statement for MyClass in the following case. Also there must not be any other explicit declarations of MyClass in the rest of the class. It seems like javac should not allow the import to be missing.
public class MyClassDao {
public List<MyClass> getAll(){....}
}
// no import needed here for MyClass
public class RandomService {
....
void process(){
myModel.setMyClassList(myClassDao.getAll());
}
}
As the Java Language Specification 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.
You are not referring to the name MyClass, so no import statement is needed.

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