How do I import packages in Java? - 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

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
}

Java packaging and import - where is my error?

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

Java package how to access a class in above folder

Here's how my directory look like:
practice(folder)
GraphTester.java
graph(folder)
Digraph.java
algorithm(folder)
TopologicalSort.java
I want to use graph.Digraph and graph.algorithm.TopologicalSort from GraphTester.java
What I try is this:
package graph;
public class Digraph
{
...
}
package graph;
// package graph.algorithm; <-- also doesn't work
public class TopologicalSort
{
...
private Digraph graph; // doesn't work
}
My question is, how can I use Digraph from inside TopologicalSort.java?
==== Update===
I tried the following, but still not working
package graph;
//package graph.algorithm; <-- this also didn't work
import graph.Digraph;
public class TopologicalSort
{
...
private Digraph graph;
}
I updated how the directory look like above. My intention was to use GraphTester.java as an outside class and not make it related to the package graph and graph.algorithm. But, it seems like putting it under the folder practice is causing the problem.
Put import practice.graph.Digraph; under your package declaration in TopologicalSort.java.
Make sure the package declaration for TopologicalSort is package practice.graph.algorithm;, it must match the directory structure.
package statement is used to create a package.
In order to use a package you need to use the import starement under the package statements follow by the name of the package you want to use.
You can also "import inline" packages, just a way to use clases or interfaces without import is to write the full path when you use it.
graph.algorithm.TopologicalSort ts = new graph.algorithm.TopologicalSort();
You can read the documentation here
add import statements.
import practice.graph.Digraph;
import practice.graph.algorithm.TopologicalSort;

How to include class from another file into your main file

I have two files : JavaAssignment1.java && KeyBoard.java
In Keyboard.java , there is a class called Keyboard
In C++ , it would be #include "foobar.h"
I have tried import Keyboard.java but it doesnt work , i have the following error :
package Keyboard doesnt exist
How do i include the Keyboard class in my JavaAssignment1.java so that i can use the KeyBoard class . ???
If in both classes you have no package declaration at the top or it is package javaassignment1; then you do not need to import anything. They have the same class path.
When you import a package you do something like
import packageName.*;
If you import particular class from package:
import packageName.particularClassName;
If classes are in same packages you don't have to do imports.
Also please read on access modifiers and access levels.

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