in most code examples I see people doing this.
import javax.swing.*; // for the frame
import java.awt.*; // for the checkBox and the label
import java.awt.event.*; // for the checkBox listener
If I am correct when we say import java.awt.* it imports everything inside it, so there wont be a need to say import java.awt.event.*; or is there a speed improvement? can anyone also explain what importing a library does, is it importing a simple text class to be included in the source or telling jvm to include the byte code of whatever is imported? so importing in java does nothing but switch the namespace, so I dont have to type long class names?
Forget the term subpackage. Do it quick. It does not exist in java world.
java.awt is a package (namespace), java.awt.event is another one and they have nothing in common. Their names share some characters, but the packages are totally unrelated. The import statements imports a class or some classes from exactly one package (namespace). If you need classes from a different package (namespace), then you have to add another import statement.
BTW, in response to a comment to another answer: You do not have to use import statements. If you don't use them, you simply have to use the fully qualified classnames in your java source file (except: classes from java.lang and the current package are imported automatically). So import could be considered as a convenient way to keep the code readable.
Importing is not required in order to use a class in your source file.
The line...
import java.awt.*;
...doesn't mean that all subpackages will also be imported. You have to explicitly import every package. As an example, importing java.* doesn't give you the entire java library.
For what it's worth, I recommend importing the specific classes only unless you have good reason to use *.
Importing a package doesn't import its subpackages.
Importing is about switching the namespace, too.
If you only had import java.awt.* and you were to use the class java.awt.Outer.Inner, everywhere in your code you have to refer to it as Outer.Inner.
By contrast, when you say import java.awt.Outer.*, you can refer to the inner class as just Inner.
Importing is just a compile time feature, in bytecode you will find only direct references to specific classes, everytime its instance is used.
"import" constructs exist just to eliminate use of full class name everytime.
Related
import java.*;
Why cannot I do this import? Instead of importing all classes in a particular sub-package of the package 'java', I tried to import all the sub-packages under the the 'java' package.
How can I import all the built-in classes?
There is no such thing as sub-package in java.
java.util.stream is not a sub-pacakge of java.util.
Therefore import java.util.* doesn't import the classes of java.util.stream.
To import all the built in classes, you have to import them one package at a time. It's a better practice, though, to only import the classes that you actually need.
Because import some.example.Type; is only to import types not the packages. import some.example.*; means you're importing all the Types contained inside some.example package not the other packages inside it.
This is because import means the code of that file will be available for your program at run time and package itself doesn't contain any code. It contains files which have the code.
That's why you can't import all the built-in code in a single import statement. At max you can import in a single statement is all the code available in different files within a package and you know the way import some.example.*;
As you can read in this link on Oracle docs, under the heading Apparent Hierarchies of Packages:
At first, packages appear to be hierarchical, but they are not. For
example, the Java API includes a java.awt package, a java.awt.color
package, a java.awt.font package, and many others that begin with
java.awt. However, the java.awt.color package, the java.awt.font
package, and other java.awt.xxxx packages are not included in the
java.awt package. The prefix java.awt (the Java Abstract Window
Toolkit) is used for a number of related packages to make the
relationship evident, but not to show inclusion.
Using a wildcard when importing classes may clutter your classes namespace so if you'll have a class named ClassA in more then one import (e.g. import com.example1.* and import com.example2.* where ClassA is defined in both and you need only the implementation in com.example1) you will have a conflict so only import what you really need to use.
Most IDEs allows you to easily organize your imports so only the classes that you really need to use will be imported
It is not like what you think, consider when you want to use ChannelHandler interface you could do either import io.netty.channel.*; or import io.netty.channel.ChannelHandler; but you cant use import io.netty.*; and that why you cant import java.*;
You can import java.util.* it work fine but when you import specific class instead of all class it take less time in importing but if you are importing too many classes from same package you can import all classes using package.* and it takes less time for jvm to fetch instead of taking one by one.
By convention you can use import package.* if you are using too many classes from same package.
Why? Because Java by design is divided into different packages. This way you can't pollute your namespace with all the classes that exist, and you have to actually specify what classes in which namespace you intend to use.
In java, packages don't relate to each other - there is no hierarchy (even if it would look like it), just flat names of packages.
As to how you could circumvent that, I'd think the only way would be to iterate all packages and generate your java code based on that iteration. There is no straight way to do it.
In any IDE, when working with a class in a package and I need to use a class from another one, I have to import it. Why doesn't the IDE just automatically import the packages so there is no need to do it manually?
Because sometimes different classes have same 'short' name so the IDE does not know which one you meant. For example if you copy-paste code into your IDE containing Date, it does not know if you meant java.sql.Date or java.util.Date. In these cases the IDE will offer you to choose from all available classes with that name.
First, In Eclipse, you can use Ctrl-SHIFT-O to automatically import anything you need.
From the java package tutorial:
For convenience, the Java compiler automatically imports two entire
packages for each source file: (1) the java.lang package and (2) the
current package (the package for the current file).
The IDE will not import all your packages because 1.
You may not need them, why import them?
More importantly, you can have classes in different packages with the same name, then this will cause name ambiguities.
If a member in one package shares its name with a member in another
package and both packages are imported, you must refer to each member
by its qualified name. For example, the graphics package defined a
class named Rectangle. The java.awt package also contains a Rectangle
class. If both graphics and java.awt have been imported, the following
is ambiguous.
This would cause unnecessary headache making sure you are always referring to the correct package. By importing packages and classes yourself, you can be sure that you are always using the specific class that you intended to.
This is my thought. Think this way,
Suppose you have two classes having the same name in different packages like this, then how does IDE resolve the package import? How does it know which SendFormAction to be imported?
So generally IDE's give you an option to import the packages [cntrl+shift+O in eclipse IDE]
and if there is confusion, it will ask you which one to import.
com.xyz.action [package]
SendFormAction.java
com.abc.action [package]
SendFormAction.java
Classes in tests package cannot find classes in default package.
If I move to classes in default package to tests, the errors disappear.
I'd like to know the reason of these errors.
To have access to classes from another package, you should import this package.
But according to JLS you can't do it with default unnamed package.
Because the class is situated in the different package and should be available to the current class via import directive. And because the class you want to import is in the default package that has not name, you can't do that, rather than move to the named package and apply the above.
Check if you have a dependency on that module in which the AddChild1_wy_v1,java is located
If it's a library then check if it is imported to your project and can be used
Import the class to the class from which you are trying to use it
???
Profit
I think it is a bad idea to put classes in the default package. Create a new explicitly named package for you classes and move them all there. If you use Eclipse's Refactor->Move menu option, it will put in all the tedious package declarations for your classes.
Then your tests can import the package.
Can anyone please let me know the relation between a package and a sub package in java?
There is no relation between packages and subpackages. It is just a hierarchical structure for the convenience of a developer and has no further meaning to Java itself.
There is no relation between them.
package com.stackoverflow in java code just like com/stackoverflow in your operating system.
I'm sorry,my English is poor,beg your parden.
Relation between the package and a sub-package in Java
As mark rotteveel said It is just a hierarchical structure for the convenience of a developer
if you're asking about the relationship between them we might say that parent and child relationship
A **package** is like a folder. in java containing a group of related classes, interfaces, enumeration, and annotation.
**Sub-package** is nothing but defined inside of another package.
A package may have many sub-packages inside it.
We use packages to avoid name conflicts and to organize your code better, making access easy.
Define package in java:(package must be in the first code of your code)
package packagename;
package packagename.subpackagename;
import packages in java:
import packagename.classname; or import packagename.*;
import packagename.subpackagename.classname; or import packagename.subpackagename.*;
Predefined sub-packages in java:
import java.util.*;
here java is a package and util is a sub-package ( lang,net,util,awt,SQL)
How to run package in notepad
javac packagename\subpackagename\programname.java
javac packagename.subpackagename.programname
What exactly is the difference between the following commands:
import javax.swing.JPanel;
and
import javax.swing.*;
If I use second one, compiler will import all files from swing or only the needed ones, will be any difference in size of the executable? thanks in advance
The first one imports only javax.swing.JPanel class.
The second one imports all classes which are present in javax.swing package, excluding the classes in its subpackages, such as the ones in javax.swing.event package, etc.
The import keyword does not literally import the given classes. It basically just points the compiler to the classes which are to be present in the classpath in order to be able to locate the dependency classes and thus successfully compile the code. The size of the compiled class depends on the size of the sole source code (this includes the import statements), it does not include the size of the imported classes.
See also
Java tutorial - Using package members
First of all, in Java you don't produce executables (well, you can, but not directly). At run time, the JVM will load whatever is needed for the program to run from a library that is already on the disk, so your executable won't grow.
Having a catch-all import merely reduces the need to explicitly list everything you are importing. It's actually often discouraged because that could later create conflicts (e.g., what if you were already obtaining x.y.Foo and now there is also w.v.Foo in your w.f.* import).
Some IDEs (such as Eclipse) can fix the latter into the former for you automatically.
There is no difference in terms of execution of your application. Only difference is during the compile time it may be a bit (unnoticeable in most cases) slower. A lot of people prefer the .* over fully qualified packages because of code readability.
I asked this question myself once too and I found this explanation to be pretty good.
The first one will import that specific package but the second one will import all packages in swing. The first one will be smaller because it is only one package.
for example, import java.lang.* will import all the classes that exist in package java.lang (but no subpackages). By all I mean any class we really use in project, nothing what is not required will be imported (so I was taught).