Is there a succinct way to import in Java which would be equivalent to the following statement:
import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
I know about this:
import android.view.View.*;
But I would like to be able to control what I import, and not just import everything in the View namespace.
ANSWER: The answer is No.
No, there's no quicker way to only import some constants. You can get them all, or you can list each one you want separately.
Actually there is technically a third option, not that it's necessarily better. You do have the option to import none of them and use their fully qualified name each time you refer to them.
Related
Many times I'm writing my java code, import statements be like
import java.util.Scanner;
import java.util.ArrayList;
But when it is required to import more classes from java.util (or any other package), is there a way to import all the required classes in one line.
Like the code below
import java.util.Scanner,ArrayList; OR
import java.util.{Scanner,ArrayList};
Anyhow the above two lines didn't work. Is there a way to do so ?
Java language does not support that.
As far as importing top level classes is concerned, you have two options:
import package.path.ExactClass
or
import package.path.*
First one imports one and only one class, second one is wildcard import that imports all classes in that package.
import package.name.Class; // Import a single class,
// "its called Importing a Package Member"
import package.name.*; // Import the whole package,
// its called "Importing an Entire Package"
For your case,
import java.util.*;
Learn more at official doc
Does Java support grouping the imports like following:
import package.{Class1,Class2}
I know that * operator imports sub packages but I want to import specific ones.
In Rust or some modern languages it is supported like following:
use package::{Class1, Class2};
Are there any alternative instead of writing each import line by line specifically like this?
import package.Class1;
import package.Class2;
Java Language Specification, section 7.5. Import Declarations, shows:
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.
[...]
A single-type-import declaration (§7.5.1) imports a single named type, by mentioning its canonical name (§6.7).
A type-import-on-demand declaration (§7.5.2) imports all the accessible types of a named type or named package as needed, by mentioning the canonical name of a type or package.
A single-static-import declaration (§7.5.3) imports all accessible static members with a given name from a type, by giving its canonical name.
A static-import-on-demand declaration (§7.5.4) imports all accessible static members of a named type as needed, by mentioning the canonical name of a type.
As you can see, it's either a single named type, or all accessible types. No syntax for a list of types.
Side note: I almost never look at the import statements of my code. I let Eclipse manage that for me (Source > Organize Imports... (Ctrl+Shift+O)), so I don't really care about having many single-type import statements. The section with the imports is collapsed anyway, so I don't even have to scroll past them. Oh the joy of using a good IDE.
In java, only supported ways to import multiple classes are as follows :
A - import individual classes
import package.Class1;
import package.Class2;
B - import all classes in a package or subpackage
import package.*;
import package.subpackage.*;
Refer Oracle doc for more information : https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
No. Java doesn't have a construct to import a set of select classes using one statement. You either import all types from the package, or you import them one by one.
Using * lets you import all classes from the same package (not to import sub-packages, as you mentioned):
import package.*; //Both Class1 and Class2 are imported
import static package.Class1.* //All static members of Class1 are imported
The first form import package.* is usually discouraged because of the increased potential for name clashes (same class names from different packages). This is probably where import package.{Class1,Class2} would have helped, but there's no such construct in Java.
Why can't I import java.util.Objects.equals? I would like to be able to use
equals(foo, bar)
Instead of
Objects.equals(foo, bar)
I can import other static methods in this way. Thanks!
Edit:
This question was poorly phrased. The real issue was that IntelliJ did not let me auto-import.
I'm able to import it, it's a static method so you need to use:
import static java.util.Objects.equals;
Well, it looks like this is currently broken in OpenJDK 11.0.6. After doing a static import of java.util.Objects.equals the compiler complains with
method equals in class java.lang.Object cannot be applied to given types
My professor gave me the java.util.Arrays.sort static method and it shows an error that the import cannot be resolved. This does not happen to any of my other methods. Is this written right?
import java.util.Arrays.sort;
Edit: Does the .sort need to do something? I changed it to
import java.util.Arrays;
and there were no errors.
Importing in Java involves either importing a member class of a package, in this case (java.util is the package, Arrays is the class)
import java.util.Arrays;
or importing the entire package, which would be
import java.util.*;
There is no concept of importing an individual class method, which is what
import java.util.Arrays.sort;
was attempting to do , because sort is just one method of the java.util.Arrays class (there are many other methods of that class).
So if you attempt to do that, you will get the error message you did.
I have always had a question about java.lang.Math: (It might by very basic)
Why do I have to Math.abs(-100) and can't to abs(-100)?
I figure that Math is a class. And abs is a static method. But why can I not simply import java.lang.Math and use abs(-100)?
You can import all the methods in Math:
import static java.lang.Math.*;
or just the one method you want:
import static java.lang.Math.abs;
Normal imports just import classes, making that class available via its short name.
abs is a static method and in order the compiler knows where it's defined, you have to specify the class (in your case - Math).
Note that you could do a static import on Math.abs and then you'd be able to just do abs(-100) instead of Math.abs(-100). In this case you'll have to add an import statement like this one:
import static java.lang.Math.abs;
Note also that in Java, unlike JavaScript and PHP, there aren't any public functions, which is why import statements are important.
java.lang.Math is statically imported in every Java Class.
static import java.lang.Math;
Every class of the java.lang package is imported that way.
As you know everything in java is within the class. So their can be only two alternatives.
Static Function and
Non Static Function
And java.lang.Math is a utility library. Creating object of this is not worth for you. so Java guys created all the functions static in this library.
And for your question you can call a member function directly if and only if they are member of same class.