Static import with same static variable names - java

I am doing a static import of members of class Long and Integer:
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Long.MAX_VALUE;
Now if I am trying to use this variable MAX_VALUE and print it I will get an error:
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Long.MAX_VALUE;
public class StaticImportDemo2 {
public static void main(String[] args) {
//Error :: The field MAX_VALUE is ambiguous
System.out.println("Print without static import Integer.MAX_VALUE "+MAX_VALUE);
}
}
This is fine. To remove the error i will have to remove one static import to resolve this ambiguity .
The main issue I am getting is, if I use wildcard * with Integer class
static import, the class gets compiled with no errors:
import static java.lang.System.out;
import static java.lang.Integer.*;
import static java.lang.Long.MAX_VALUE;
public class StaticImportDemo2 {
public static void main(String[] args) {
System.out.println("Print without static import Integer.MAX_VALUE " + MAX_VALUE);
}
}
The ambiguity must still exist. Why does this compile with no issues?

Why does this compile with no issues?
Because the Java Language Specification says that it does. See chapter 6 and 7, but particularly from 6.4.1:
A type-import-on-demand declaration never causes any other declaration to be shadowed.
A static-import-on-demand declaration never causes any other declaration to be shadowed.
And that’s probably because it’s very convenient to be able to wildcard-import entire packages, but sometimes you’ll have to resolve conflicts. It would suck (particularly in pre-IDE days) if the only alternative was to explicitly import every item. So specific (non-wildcard) imports were given precedence. That way, you just specify which you mean for the ambiguous items you want to use.

Related

Using pre-defined class names in java

I'm fairly new to Java. I learnt that the names of predefined classes in java are not keywords and thus we can use those as identifiers.
I tried out the following code: (I know that that import is redundant)
import java.lang.*;
class Process{
public Process(){
System.out.println("Constructor of Process");
}
}
public class NewMain {
public static void main(String[] args) {
Process p = new Process();
}
}
and the out put was:
Constructor of Process
However, when I replaced import java.lang.*; with import java.lang.Process;, I got an error cause it tried to instantiate the predefined Process class. I had assumed that the program will check scope by scope and will thus execute the userdefined class constructor in both cases. How am I wrong?

Are java imports used professionally?

I'm currently self studying Java, and am not sure with this "import" thing. It adds classes(not sure), and has methods that would serve a different function relative on the type of class I declare from the "import"ed thing.
I'm curious if import is frequently used for work, or is this relative to the user if he/she would want to use import or not.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner ThisObject = new Scanner(System.in);
}
}
Java import is a compile time feature that allows you to omit the package name when programming. There is no byte-code import, the compiler will replace
Scanner ThisObject = new Scanner(System.in);
with
java.util.Scanner ThisObject = new java.util.Scanner(System.in);
As for how common it is, I would say extremely. However, there is another form of import called static import which allows you to bring static methods and fields from another class into your current namespace. static import is not very common. Finally, Java variable names start with a lower case letter (by convention).
import static java.lang.System.in;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner thisObject = new Scanner(in); // System.in through static import
}
}

Why can you import a class with the same name as a nested class?

Consider the following code:
import java.util.Calendar;
class Demo
{
class Calendar {}
public static void main (String[] args) {
// System.out.println(Calendar.DAY_OF_WEEK); // Would be an error.
}
}
This code compiles fine; but if you refer to Calendar within Demo, you are referring to Demo.Calendar, not java.util.Calendar.
The import is clearly redundant; but it seems strange that it is allowed, considering you're not allowed to import a class with the same simple name as a top-level class defined in the same compilation unit (per JLS Sec 7.5.1):
import java.util.Calendar; // error: Calendar is already defined in this compilation unit
class Calendar {}
Is there a practical reason why such an import as in the first code example would not be a compile-time error?
The only case I can come up with is where you have a twice (or more) -nested class with the same name as the import:
import java.util.Calendar;
class Demo {
static class Nested {
static class Calendar {}
static void useNested() {
System.out.println(Calendar.class); // Demo.Nested.Calendar
}
}
static void useImported() {
System.out.println(Calendar.class); // java.util.Calendar
}
public static void main(String[] args) {
Nested.useNested();
useImported();
}
}
Ideone demo
In this case, the nested Calendar isn't automatically visible outside the scope of the Nested class, so the imported Calendar class is used outside, e.g. in the useImported method.
I wouldn't really describe this as a "practical" use, though - it's just plain confusing as to which is used in each context, and definitely worth avoiding. It still interested me that this case exists, though.
I suppose there is another similar case:
import java.util.Calendar;
class Demo {
static void useImported() { ... }
}
class Demo2 {
class Calendar {}
static void useNested() { ... }
}
(where these classes are in the same compilation unit). Basically the same idea as above.
I suppose, if you import a class, it's visible in the global space of that compilation unit. But if you name your compilation unit or top level class the same as the import, then you are basically conflicting it with the import and hence it'll be ambiguous for JVM to know which is which. and since its compiling a class, it'll give an error for import.
Also, when it's inside another class, you are shadowing the import over there. It's just similar in the way as global/class level variables and method level variables hiding them if defined with the same name.
Hope this helps.

Is it possible to do static import on whole class by single declaration in Java?

Can in java declare?
(1) import static com.example.core.MyClass;
where:
public MyClass{
public static double divide(){
.....
}
public static double add(){
.....
}
}
and in the class where use static import to use freely:
devide and add methods without the prefix - name of the class.
From what I read we can do:
import static com.example.core.MyClass.divide;
import static com.example.core.MyClass.add;
But Can we do it in one statement as I mentioned in (1)
Yes, use
import static com.example.core.MyClass.*;
You can use import static com.example.core.MyClass.*;
MyClass.* will load all the properties inside the MyClass you won't need to access properties as MyClass.divide....

Java Packages - Import vs Explicit Inclusion

I thought I reasonably understood the use of packages but am experiencing an ostensibly trivial issue when attempting to use a method from an imported package.
I have three files in the following directory structure:
Tester.java
approach1\Approach.java
approach2\Approach.java
Their code is as follows:
Tester.java
import approach1.Approach;
public class Tester {
public static void main(String[] args)
{
approach1.Approach.sharedMethod("TEXT");
sharedMethod("TEXT");
}
}
approach1\Approach.java
package approach1;
public class Approach {
public static void sharedMethod(String approachText)
{
System.out.println("Approach Text: " + approachText);
}
}
approach2\Approach.java
package approach2;
public class Approach {
public static void sharedMethod(String approachText) { }
}
As you can likely guess, I'm trying to elicit different responses from the different approaches based on what package/class is imported. The problem I encounter is within Tester.java. The first, explicit line works fine whereas the second, imported line (sharedMethod("TEXT")) throws an error of "The method sharedMethod(String) is undefined for the type Tester". I don't understand as I have imported one of the packages, so the method should be visible.
Any clarification would be appreicated as I'm a Java newb. Thanks!
You could import your static method shareMethod like this
import static approach1.Approach.sharedMethod;
the standard kind of imports that you have used only import the classes - so everything within a class must be referenced using the class name. just use:
Approach.sharedMethod()
and now the compiler will be able to know which method to use all depending on which Approach class you have imported at the top.
Just to clarify:
import approach1.Approach;
public class Tester {
public static void main(String[] args)
{
Approach.sharedMethod("TEXT");
}
}
is different from
import approach2.Approach;
public class Tester {
public static void main(String[] args)
{
Approach.sharedMethod("TEXT");
}
}
You should only specify the class name and leave it to the package import statement at the top to determine which package to find the class/methods from.
You only need to explicitly mention the package in the main program body if there is a conflict in names or if you have not imported anything.

Categories

Resources