Java packaging and import - where is my error? - java

Trying to learn Java from Bruce Eckel's book, I don't understand why the compiler doesn't find the library I want to import. I have first done this on Windows/Cygwin and now on Centos 7, using OpenJDK 1.8.0. Same result on both platforms.
This line:
import static t.b.u.*;
causes compiler error
$ javac TestPrint.java
TestPrint.java:2: error: package t.b does not exist
import static t.b.u.*;
^
I agree that package t.b doesn't exist, but I actually wanted to import package t.b.u. Why does the compiler ignore the u?
CLASSPATH is set as follows:
$ export|grep CLASS
declare -x CLASSPATH="/home/bbausch/thinking-in-java"
The package is a single file:
$ cat /home/bbausch/thinking-in-java/t/b/u/Print.java
package t.b.u;
import java.io.*;
public class Print {
... etc ...
The error is probably so obvious that I don't see it. Can somebody help?

This is specifically related to the Java Language Specification: https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5.3
These two lines inherently ask for different things:
import static t.b.u.*;
This statement asks to import all static methods from a class named u from a package t.b.
import t.b.u.*;
This statement asks to import all classes underneath t.b.u.
Static imports target a TypeName only. Normal imports target a package, or a specific class.
The roughly equivalent general import to the static import would be this:
import t.b.u;
This asks to import just the class u from a package t.b.
In your specific example, you'd probably want this statement to import all static methods of the Print class.
import static t.b.u.Print.*;

Related

Import java.nio.file.Files and com.google.common.io.Files in 1 file is not allowed

I'm very new to java I need to use a different features from MAVEN dependencies but they have a same name like this,
import java.nio.file.Files;
import com.google.common.io.Files;
I do not allow me to import. I will throw error like
The import com.google.common.io.Files collides with another import statement
Can this be solved ? Thanks a lot.
When you have to use two classes with the same name, you have to import one and use the fully qualified name of the other one in the code.
For example, leave the first import. And when you want to create one variable of each type, you do the following:
import java.nio.file.Files;
public class MyClass{
Files files; //This variable uses the imported type
com.google.common.io.Files ioFiles; //This variable uses the explicit type
}

Why do I get different compilation result depending on java imports and static imports sequence order?

I've faced an issue with compilation, but cannot understand why it occurs.
Actually much time was spent to understand where the reason is (it was far from obvious in a "crap" project), but after reproducing that error I greatly simplifies all the code to show a little example especially for you:
Package structure:
com.company
|
----Main.class
|
----maker
|
----Maker.class
Maker.class
package com.company.maker;
public interface Maker {
}
Main.class
package com.company;
import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
import com.company.maker.Maker;
public class Main {
public static void main(String[] args) {
System.out.println(STRATEGY1.name() + STRATEGY2.name());
}
static class MakerImpl implements Maker {
enum Strategy {
STRATEGY1, STRATEGY2
}
}
}
And I got compilation error in Main class:
Error:(15, 39) java: cannot find symbol
symbol: class Maker
location: class com.company.Main
And if I change the import sequence from
import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
->import com.company.maker.Maker;
to
->import com.company.maker.Maker;
import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
then it is compiled successfully.
Is it normal behaviour of Java Compiler? If so I want to clearly understand why it happens.
P.S. tested using java version 1.8.0_112 and 1.7.0_80 (MacOS)
check this :
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6391197
It seems that the compiler sees the first static import and then jumps to take care of your inner class, but fails because it did not read the last non static import.
So when you change the import order, this problem does not occur, since when the compiler reads the static import and jumps to take care of the inner class because those imports are used in the inner class, the compiler is happy since it already imported the non static Maker Interface.
Wich means that the imports are evaluated eagerly.
I hope this helps.
See also Java how import order matters when import class/enum inner an inner class to see that this problem also exists with non static imports.

Android Studio - why import statement is unused/not needed?

In an Android studio library project, the following code piece gives error.
package my.package.a;
import my.package.b.Test; //this shows unused, why??
public class **Test** extends my.package.b.Test { //"Test is already defined in this compilation unit." why?
...
}
extends my.package.b.Test this line is using package b, isn't it? so why the import statement shows unused?
These two Test classes are in different packages, why does it have name conflicts??
Solution:
Refer to full name and delete import statement.
Cannot import my.package.b.Test as it's in conflict with current class name.
The import is not needed here because you're already calling out my.package.b.Test by full name. If you use the fully qualified reference to a symbol, there is no need to import it.

How do I import packages in Java?

I have a "Sprites" folder with some class files and a "Launcher" folder with some class files. I tried the following code for import:
package Sprites;
and it lead to the following
hw9\Launcher>javac *.java
TowerDefense.java:2: error: class, interface, or enum expected
package Sprites;
^
1 error
Am I doing this incorrectly? My Sprites and Launcher are in the hw9 directory, so I assumed it would work. A picture for clarification:
You can use a wildcard import to import all classes within the immediate directory:
import Sprites.*;
This opposed to something like:
import Sprites.Class1;
import Sprites.Class2;
import Sprites.Class3;
...
Generally, wildcard imports can produce conflicts and errors (for example java.awt.List and java.util.List), so usually better to avoid them.
Packages should also be lower-cased.
The error is due to syntax, usually when you see something like ...expected that is syntax error indicator.
In the class in your launcher package, include the import statements for the classes which are being referred to.
It should look something like the following:
package the.name.of.your.package;
import Spirites.NameOfclass; //quialify the import parth as is
class YourLauncherClass{
//class definition
}
Also make sure that semicolons aren't missing at the end of import and package.
Hope that helps.
Best practice is to import the specific class you require rather than importing the complete package.
import Spirites.NameOfclassRequired;
class YourClass{
//class definition
}
If you are using eclipse you can do that using CTRL+SHIFT+O When you do that eclipse imports the specific class you require. For an instance if you using an ArrayList rather than importing java.util.*; it will import java.util.ArrayList;
If you need multiple classes from a package then for sure you can import the entire package

Import a single class in Java

Simple question but even though googled it a lot I could not find the answer.
Is it possible to import a class outside a package?
Let's say I have 2 folders A and B with a .java file in each, is it possible by using the clause import to import the class contained in A? import A.Aclass ? or it's mandatory using package syntax whenever there is the keyword import?
Yes it is possible to import the class with the import statement. For better understanding let's assume that you have three folders foldera, folderb and folderc where foldera contains a .java file named "ClassA.java", folderb contains another .java file named "ClassB.java" and folderc contains a .java file named "ClassC.java". Now, if you want to uses the member data and operations of "ClassA.java" in "ClassC.java" you can use the import statement as shown below:
import foldera.ClassA
If you want to use the member data & operations of "ClassB.java" in "ClassC.java" it is also possible with the import statement
import folderb.ClassB
As per the java source file declaration rule, if the class is a part of a package, the package statement must be the first line in the source code file, before any import statements that may be present. In this example, the first line of "ClassC.java" source file must be package folderc since it is located in folderc. Similarly, the first line of "ClassA.java" source file must be package foldera, and the first line of "ClassB.java" source file must be package folderb.
Hope now you are clear with the concept!
Thank you...
Well, if the class is defined to have a package a; then you need to import the class with the package name. If you have two packages which contain a class with the same name, then in your class which needs to invoke each of them, you will need to use a fully-qualified name. For example:
import a.Foo;
import b.Foo;
public class Bar
{
public static void main(String[] args)
{
a.Foo aFoo = new a.Foo();
b.Foo bFoo = new b.Foo();
}
}
Alternatively, if you have two packages with classes of the same name, you can simply skip importing them, but rather -- using them by their fully-qualified names (FQN-s).
If the class does not have a package ...;, then simply import it as:
import Foo;
However, if you have two packages (from different libraries) which contain classes with identical FQN-s, then the first one on the classpath will be picked.
Please, bear in mind that the convention for naming packages is to use lowercase letters and for classes -- the name should start with an upper case letter for each word in the class' name.
Yes it is possible.
If you have the following:
Package: PackA
Class: ClasA
Do:
import PackA.ClassA; //Import the class
OR
import PackA.*; //Import all the classes within the package
yes it is possible just import the package
syntax
import pck.ClassA or import pck.*
Yes, you have to use package syntax.
importing all class inside folder A.
import com.pack.A.*;
importing specific class inside folder A.
import com.pack.ClassName;

Categories

Resources