Why can't I import java.util.Objects.equals - 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

Related

Why won't abs() in java work?

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.

import static method in xtned (generated java)

It is possible to import just one static method, or member in extend? I know that it is possible to import all static methods by *, like in this example ...
import static example.Types.*
But I need only one.
Yes it is possible.
import static System.out;
You can then merely write
out.println("Doobedoo");
Which will save you from typing the System.out part several times over but is potntially confusing for anyone trying to follow you code.
Use it sparingly and generally only for constants ( static final ).
If what you're looking for is something like the using keyword from C# or C++ java doesn't have that feature.
Java 5 added an import static option that allows static variables (typically constants) to be referenced without qualifying them with a class name.
For example, after
import static java.awt.Color;
It would then be possible to write
Color background = RED;
instead of
Color background = Color.RED;
Example above have been taken from: Java: packages and Import.
The site used for the example seems to be out of date, sorry any trouble this can cause
See How java import works for more detail.

What is static imports and what is its importance in java

While reading some java books, I came to know about static imports.
I have some doubts in my mind.
What is static imports.
When and why to use it.
Explaination with examples will be helpful.
One example is JUnit tests
import static org.junit.Assert.assertEquals;
...
assertEquals(x, y);
Imports are typing shortcuts. A "regular" import is a shortcut down to the class level...
import java.util.List
Let's you just use
List l;
Instead of
java.util.List l;
A static import is a shortcut down to the method level. The method must be static, since there is no instance to associate with it...
import static java.lang.Math.abs
Lets you just use
x = abs(y);
instead of
x = java.lang.Math.abs(y);
Imports do not effect your compiled output or running code in any way. Once something is compiled there's no way to tell if the original source had imports or not.

How do I use enums without specifying the enum class each time?

I'm looking for a faster way to use enums as parameters. like if I have a method
void junk(EnumVar var){
Instead of typing EnumVar.VAR_1 I'd rather just type "VAR1" and then use content assist to do the rest, or have VAR_1 be acceptable alone, or even just use content assist give me some options. I know eclipse will do this when I type enumvar....
Is there any way to have eclipse help me work with enums faster? sort of how it does with switches.
Static import may be what you need.
For instance try import static foo.bar.MyEnum.VAR_1;
Yes, include a static import with the rest of your imports, like this:
import static my.package.EnumVar.*;

Java: Succinct way to import multiple constants

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.

Categories

Resources