What is difference between java.awt.* and java.awt.event.*?
It's just two different packages.
When you say import java.awt.* it imports only those classes which belong exactly to java.awt package, not to its subpackages.
That's why when you want to use events you have also import java.awt.event.
Do you mean while importing? They are different packages. They aren't necessarily related. Importing java.awt.* doesn't import java.awt.event.*
Related
Java Servlets use usually the following import statements:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
If I get it right then javax.servlet.* imports everything inside the package. And because .http is a subpackage of .servlet:
Isn't the third statement unnecessary?
import javax.servlet.* should include .http already.
Or is my assumption wrong. Then please correct me.
No, Java don't do that.
Importing javax.servlet.* imports all of the types in the javax.servlet package but not the types declared in javax.servlet.http.
See tutorial (Apparent Hierarchies of Packages section)
I wanted to import a java package "TestPackage" into jython script,
import TestPackage
print TestPackage.SampleClass().getMessage();
I am getting the following error,
ImportError: No module named TestPackage","errorPoint":"","lineNumber":"1","errorPointMessage":"in <module>\nImportError: No module named TestPackage
When I use from TestPackage import SampleClass works fine. If I execute from <pacakage> import <anyclass> statement once, then import <pacakage> works without the import error.
I tried setting PySystemState.add_package("TestPackage"), this works but leading to memory leakage.
Is there any alternative way to make use of import TestPackage ? How can I use "import <packagename>" in jython without memory leakage?
This is due to a difference in Java and Python.
In Java you can import classes, and static import constants and static methods. You cannot import a package. import java.util; is a compile error, but import java.util.Collections; or import java.util.Collections.*; are fine. A Java package is a way of organizing a bunch of classes but doesn't isn't a class with a constructor, methods, or constants. I like to think of a Java package as a zip file containing all the classes that belong to that package. A Java package is merely a namespace.
In Python you can import modules, classes, and from-import classes, functions, and constants. However, a Python module is a lot more like a Java class than a Java package. It is not merely a "zip file of classes", but a class that contains subclasses. A Python module is a collection of scripts in a folder with an init.py, which defines classes and methods for that module. Thus, you can do things like import collections. Python also lets you do import collections.OrderedDict, from collections import OrderedDict, and from collections import OrderedDict as OD.
I hope this explains why you can't import your Java package. You have to import something (probably a class) from that Java package in order for python to create a variable.
If, in import java.* the * includes all the packages, then why do have to write import java.lang.* and import java.util.*?
import packagename.*;
imports all classes, interfaces ect. from the package packagename but not "subpackages", i.e. import java.*; imports all classes from the java package, but no classes from java.util. Since there are no classes in the java package, using import java.* imports nothing.
Even though typically packages map to file system directories, there is not a hierarchical relationship between packages. A package com.initech is not the parent of com.initech.tps, they are two separate packages with no relationship between them. You can't refer to multiple subpackages with a wildcard because as far as the compiler is concerned there is no such thing as a "subpackage". java.lang is a package, java.util is a package, but java is not a package.
Let's take an example
Suppose you have one package called base.. And inside it you have package called child1..
Base package also have two classes in it..b1 and b2
And your child1 package also have some classes into it..c1,c2..
Then when you write
import base.*
it only imports classes of base package i.e b1 and b2 not the child package classes...
And when you write
import base.child1.*
It imports all classes in child1 package....
If I say:
import java.awt.event.ActionListener;
I get the ActionListener Class.
If I say:
import java.awt.event.*;
I get the event class including ActionListener?
Or better yet:
import java.awt.*;
I thought that if you included a class, like in the last two example, that you effectively imported that class and inherited all of its subclasses. But, when I use only the last line, for example, Eclipse often shows errors saying it cannot resolve certain items and suggests I include both the java.awt and java.awt.event.
The "wildcard" imports in Java only work to the immediate level at which classes are implemented.
That is, if you have classes A, B and C, with fully qualified names:
com.foo.bar.A;
com.foo.bar.B;
com.foo.C;
then importing com.foo.bar.* will allow access to A and B without further ado; but C will NOT be available.
In the same vein, importing com.foo.* will readily have C available, but not A and B.
Now:
I thought that if you included a class, like in the last two example, that you effectively imported that class and inherited all of its subclasses.
It does not. Even if B "inherits" A, if you choose to use the fully qualified import com.foo.bar.A, it WILL NOT automatically import com.foo.bar.B. You'll have to import B separately. Which makes sense: nothing forces implementations of an interface or abstract class to be in the same package as their base interface/base class, for one; and in the same project, you may have two classes named B, in different packages: what should the compiler do?
Now, according to coding style conventions, which you either make up for yourself or have to obey in your work environment, such wildcard imports may be purely and simply forbidden, and you'll have to import A and B separately. As to static imports, they have other problems...
Finally, note that by default, you can use all of java.lang.* without having to declare an import.
java.awt.* is different with java.awt.event.*, the first one will import all the classes within java.awt package, while the second one will import all the classes within java.awt.event, the import function will only import classes, not packages.
From the Java Tutorials: 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.
Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color, java.awt.font, or any other java.awt.xxxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:
import java.awt.*;
import java.awt.color.*;
Remember that the import statement is just for convenience. It is there to make it possible for you to use the short name of a class rather than the fully qualified name.
The package name structure in Java corresponds to a directory structure. So you can think of it as a directory named java and in that directory there are several other directories such as awt and io etc.
When you say
import java.awt.*;
you are basically saying that you want to use the short name for all the classes in the directory named awt inside the directory named java. So if you use a class name in your code like this:
List mylist;
Then the compiler will attempt to find either a class named List in the current package or a class named java.awt.List.
So if you have a directory inside the awt directory called event and you have a class named ActionEvent in that directory, the fully qualified name is:
java.awt.event.ActionEvent
and the import statement above does not help. Hence the reason for needing another import statement
import java.awt.event.*;
Now, if you use the class ActionEvent the compiler looks for a class named ActionEvent in the current directory, or java.awt.ActionEvent or java.awt.event.ActionEvent until it finds one.
From Sun's documentation on Using Package Members:
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.
Importing java.awt.* imports all of the types in the java.awt package,
but it does not import java.awt.color, java.awt.font, or any other
java.awt.xxxx packages. If you plan to use the classes and other types
in java.awt.color as well as those in java.awt, you must import both
packages with all their files:
import java.awt.*;
import java.awt.color.*;
If you have time, you will probably do well to read through the entire Packages trail from sun which goes over the basics and more complicated concepts thoroughly. From the language in your question, where you refer to the second import statements as including particular classes, it sounds like you're not understanding that you're actually referring to packages.
I've been struggling with my first regex. During the compile, Pattern and Matcher kept getting cannot find symbol errors.
I just changed import java.util.* to import java.util.regex.* and it works like a dream.
I was under the impression that importing java.util.* would also bring in java.util.*.* etc. Is that not the case? I can't find any documentation that addresses this specific question....
No, package imports only get the direct classes in that package (java.* will not import everything, only ones such as Java.SomeClass, not java.util.SomeClass)
Importing java.util.* will not import java.util.*.*.
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.
Ergo, javax.swing.* will not import javax.swing.event or javax.swing.event.*
Read the following blog for some friendly newbie advice.
See a link and a quoted excerpt from the link below.
http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
Importing java.awt.* imports all of the types in the java.awt package,
but it does not import java.awt.color, java.awt.font, or any other
java.awt.xxxx packages. If you plan to use the classes and other types
in java.awt.color as well as those in java.awt, you must import both
packages with all their files:
import java.awt.*;
import java.awt.color.*;