This question already has answers here:
Is this how Java package imports are supposed to work?
(4 answers)
Closed 8 years ago.
I wrote this command line:
Field [] field=Hello_World.class.getFields();
and I used the import java.lang.*; sentnce, that suppose to import all of the classes in the lang package. even so when i wrote this command line it throw me an errow and suggested to use the following import sentence: import java.lang.reflect.Field; which, to my understing, import the speciefic field class from the lang package. I dont understand why does my orginal import sentence doesn't solve this problem(It supposed to import all of the classes, doesn't it?)
You have two misconceptions:
You don't need to import java.lang. It is implicitly imported.
Importing xxx.* imports only the classes and interfaces in xxx. It does NOT import any "subpackages" of xxx; e.g. xxx.yyy or xxx.yyy.zzz.
So what you should do is get rid of import java.lang.*, and add import java.lang.reflect.*.
Aside: A lot of people (myself included) think that "star" importing classes and interfaces is a bad idea. It can cause source-level fragility; e.g. if someone unexpectedly adds a class to some package that collides with a same-named class you've imported from another package.
The java.lang.* import all classes of lang packages but not its subpackages subclass. so to import the subclasses of reflect, you have to import java.lang.reflect.Field.
Yes, that is how package imports work (and are supposed to work) in Java. For example, doing import javax.swing.; will import all classes within javax.swing. but not sub-packages and their classes.
Reflection is not in lang.* package. its subpackage to language package. You need import java.lang.reflect.* it ll work. As other answers imply that import doesn't import subpackages.
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
This question already has answers here:
What is the relationship between a package and its subpackage in Java? [duplicate]
(4 answers)
Closed 2 years ago.
To use collectors class methods like Collectors.toList() , we need to import java.util.stream.Collectors;
I have found that Collectors is a class and not a package as an answer at some places. But I still don't understand even if Collectors is a class, it is still part of java.util.* . Then why it needs to be imported seperately.
When you do the import of java.util.stream package you only import classes at the directory level specified. If you want to import classes at the level below this, you need to import that package directory as well. The import call does not recursively import everything from all directories and below, it only imports what you ask for at the level specified.
This question already has answers here:
Meaning of the import statement in a Java file
(5 answers)
Closed 6 years ago.
What does java.util.*; do? Why do we include it at the beginning of our files?
The statement java.util.*; imports all of the java.util package members so that you don't have to use a package member's fully qualified name. According to the JavaDocs here the package java.util
Contains the collections framework, legacy collection classes, event
model, date and time facilities, internationalization, and
miscellaneous utility classes (a string tokenizer, a random-number
generator, and a bit array)
Although this approach can seem more convenient and is sometimes appropriate you shouldn't always be including an import java.util.*; statement at the beginning of all of your files unless you are using a substantial amount of the members contained in the java.util package. Only include the members that you use like so:
import java.util.ArrayList;
import java.util.LinkedList;
By doing so it helps make you more familiar with each package member that you're using instead of blindly importing the whole package. The most important reason is that by using the wildcard character(*) you have a greater chance of coming across name ambiguities which can lead to errors.
import java.awt.*;
import java.util.*;
In the above code example, the class List becomes ambiguous because both packages have a List class.
Think of it like a library of methods that you now have access to. You are basically importing more functionality into your project
When you do
import java.util.*
you now have the ability to do things like create arrays, manipulate dates and so on... (https://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html)
The .* means you are importing all of the util functions like java.util.Arrays or java.util.Date...
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.
This question already has answers here:
Import package.* vs import package.SpecificType [duplicate]
(10 answers)
Closed 7 years ago.
In an exercise program I'm working on right now, it calls for a handful of imports, like so:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.net.URL;
ELI5: My question (I'm slightly past the newbie beginner tutorials, and getting into intermediate? now.) is just as the title suggests. Am I actually required to import java.awt.event.* if I've already covered it with java.awt.*?
If not, why is this the seeming convention?
An import statement ending in an asterisk only imports all classes from that package, not any subpackages. So if you have class A living in com.example, and class B living in com.example.subtype, an import statement like
import com.example.*;
will import class A but not class B. In order to import class B you'd have to add
import com.example.subtype.*;
This is to ensure that if you need a number of classes from a 'higher' package (hierarchically speaking), you don't import all classes from every subpackage beneath it which you might not need or want, some of which could have the same name leading to classname conflicts.
ie, if there is a class com.example.subtype.Type and a class com.example.order.Type you don't want an import statement like import com.example.*; to import both Types.
This is also why it is generally better to explicitly import certain classes instead of all classes from a package (as you do with the asterisk), unless you need so many classes from that package the load of imports will only serve to confuse.
You don't have to use .*
For Example, you only want to use an JFrame, you can import
import javax.swing.*;
but why import all functions of the package (not subpackages), if you only need one?
Better use this (in this Example):
import javax.swing.JFrame;
See also here: Import package.* vs import package.SpecificType