How does "import *" work in Java? - java

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

Related

Packages in java not compile

Good morning everyone!
I did a project x but all my files were out of order so I decided to group them by folders.
foldera
---ClassA.java
---ClassB.java
folderb
---Class1.java
Main.java
The problem arises when I try to compile, since in the main it appears that the classes I made are not found
I thought this could be solved by putting in the classes
package src.foldera.ClassA;
And in the others the same
package src.foldera.ClassB;
And
package src.folderb.Class1;
So in all classes
And in the main put
import src.foldera.*;
import src.folderb.*;
But I keep getting the same error even though I put the packages
It should be noted that I did not create the folders in the code editor, rather I did it in the same Windows 10 File system
What is this about? Thanks!
Assuming a standard setup where the src folder is the root of your source hierarchy, the statements should be:
package foldera;
package folderb;
The imports should be similarly shortened to:
import foldera.*;
import folderb.*;
You have to define the package in which the class is in.
As an example in classA:
package src.foldera;
And in class1:
package src.folderb;
import src.foldera.ClassA;

Jython Java Package Import Statement

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.

Why don't I need to use the import keyword when importing a library?

I imported an external JAR to my project in Eclipse, by following these instructions:
Right click on the project > Build Path > Add external archives > Choosing the JAR file from the hard drive.
The JAR file than appears in the 'References libraries' section in my project, and works fine.
However, I never need to use the import keyword in my classes in order to use the classes from the JAR. I find this weird, I thought I'd have to use import myImportedJar or something similar for this to work.
Is it normal that I don't have to use the import keyword? Did I do something wrong?
The import statement is used to be able to refer to types and their members by their simple names. You don't need to import classes that are in the same package, unless they are nested members of other classes. This is true regardless of where the class comes from, for example, if it's in another .jar.
The import keyword works on a package level. If these packages are supplied by jars or not is not a feature of the java programming language.
import only makes a name available in unqualified form in your program. The following code fragments are identical:
java.util.List<Object> list = new java.util.ArrayList<>()
vs
import java.util.List;
import java.util.ArrayList;
List<Object> list = new ArrayList<>()
Now, importing a jar file in eclipse puts the classes from this jarfile on the classpath - the total "world" of available classes for your application.

confused with package and import statements including default package

I am confused with the terminology and its use in the eclipse IDE.
It is my understanding that you can use the import keyword to reference specific classes/java files within a group of class/java files called packages. You can also reference an entire group of classes/java files by using a wildcard modifier end the end of the import statement. So why do this when you can just make the package statement at the top of the java src file instead of having to do an import?
for instance:
folder structure: myapp>graphics>.class1, .class2
instead of doing this: import myapp.graphics.*
we can do this right?: package myapp
so whey even have the import keyword? can't you just use package myapp and reference the class with say class1 example = class1();
Second, I tried to remove the package com.example.myapp and just import the classes with import myapp.graphics.* but it it gave me an error stating that "" does not match the expected package com.example.myapp. one of the fixes for it was to move MainActivity.java to a default package. I chose that option and it moved it to a default package and then I was able to use the import myapp.graphcis.*; statement while leaving out the package myapp.graphics statement without any errors.
I am confused. Also, what is a deauflt package? I read somewhere that it is bad practice to use a default package. why?
thanks.
Packages - A location where class is located. It is used to build the fully qualified class name.
Import - Used for incorporating pre-existing classes, by using this you can avail the functionality from them.
Default package - Location directly under src folder, for example: src\NoPackage.java
For example, if NoPackage.java contains import com.assertcheck.AssertTesting; then AssertTesting is references under NoPackage class.
However in AssetTesting class you can't import/use NoPackage.
According to oracle:
A package is a namespace that organizes a set of related classes and
interfaces. Conceptually you can think of packages as being similar to
different folders on your computer. You might keep HTML pages in one
folder, images in another, and scripts or applications in yet another.
Because software written in the Java programming language can be
composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and
interfaces into packages.
So, you can think of packages as organizers, which have nothing to do with imports.
The default package is no package at all. Eclipse warns you about this when you leave the field blank. Using it is bad practice because
Using the default package may create namespace collisions. Imagine you're creating a library which contains a MyClass class. Someone uses
your library in his project and also has a MyClass class in his
default package. What should the compiler do? Package in Java is
actually a namespace which fully identifies your project. So it's
important to not use the default package in the real world projects.
Why shouldn't we use the (default)src package?

importing classes using wildcards

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.

Categories