Why do internal classes have to be imported? - java

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

Related

IntelliJ: How to use external JAR's as a library, when the JAR's have no packages

This question is related to setting up an IntelliJ environment for Princeton's Algorithms 2 course available on Coursera.
I am trying to import external libraries, as JARs, into my project. I was able to add the JARs from the Project Structure menu via Project Structure -> Libraries -> New Project Library (the green plus sign). Now I have a class under src, WordNet.java, but I can only access my external libraries using the default package (ie no package). I would like to create packages to organize my code, but how can I import the external libraries from inside a package? Is there a simple solution to directly import the JAR's, or perhaps I can use Maven or Grails? Providing a simple answer for all of my options would be great.
I have the following project structure, with a src directory, src/assignemnt1 package, and External Libraries/stdlib/stdlib.jar external library:
My class that uses the external libraries, WordNet.java, has code as follows:
public class WordNet {
// constructor takes the name of the two input files
public WordNet(String synsets, String hypernyms) {
In read_synsets = new In(synsets);
read_synsets.hasNextLine();
}
}
Where In is a class under stdlib.jar. Under the default package, I can use In without importing. Unfortunately, if I have WordNet.java under src/assignment1 (inside the assignment1 package), I cannot seem to import In and IntelliJ offers no import suggestions either. Is there a way to use stdlib.jar within WordNet.java, inside the src/assignment1 package? Or do I have to stay withing the default package?
The Java language specification forbids any imports from the unnamed, or default, package.
A type in an unnamed package (§7.4.2) has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported, and (b) static members of types in an unnamed package cannot be imported. As such, §7.5.1, §7.5.2, §7.5.3, and §7.5.4 all require a compile-time error on any attempt to import a type (or static member thereof) in an unnamed package.
In order to access these classes from outside the default package without modifying the library, you would need to use reflection.
Additionally, the reason you don't need an import when your class is in the default package, is because you don't need to import classes when they are in the same package.
I'm afraid this is impossible. Specifically, you cannot "import" the default package into a named package. Since the library you're using has its classes in the default package, your only recourse is to use the default package as well, if you want to use the library.
Of course, you could move the library's classes to a package, but that's a different story.

Java - Cannot find symbol in default package

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.

Must all java class file belong to a java package?

It seems there a 'default package' in eclipse.
What's it?
Leaving a class without a package, i.e. in the default package, is fine if you're just hacking something up for an example or something.
However, as soon as you start adding more and more classes without packages you'll soon find things getting messy.
The main reason not to use the default package is the potential for conflicts with other code. This is where it is helpful to use the "reverse domain name" style of package naming such as "com.stackoverflow.utils.MagicConverter".
Use packages.
The default package is for classes that do not have any package declared. However this is discouraged (eclipse warns you) - you can't import these classes.

Importing classes inside JAR files that are in the default package

How do I use Queue here?
I can't get the import to work because Queue.class is not in a package.
I have tried everything in this universe, and more.
http://i.stack.imgur.com/AFrrc.jpg
Your Queue class is under unnamed package (refer JLS). It can't be imported. Also, it's bad practice.
You have two options
Move your own code to unnamed package. Theoretically, both of them being in same package, you will not need to import Queue as it's class-name is suffice to find out the class.
Use JarJar - a tool to repackage your Jar files. Repackage the Jar containing the Queue class and move the unnamed package to a sensible package name. Use Fully Qualified Class Name (FQCN) to import.
I would suggest to use packages and go for option 2.
Edit 1: also see this What's the syntax to import a class in a default package in Java?

How do I resolve a class/field in a JVM compiler?

I'm making a JVM language. This language has modules (namespaces) and the ability to import java libraries. In the import section the user can import things like "java.io." or "java.concurrent." etc.
How do I know that a "File" reference in the source is in the package java.io and not somewhere else?
Because Java is statically typed the lookup need to be be unambiguous. When you import entire packages (or namespaces) your compiler will need to handle name clashes in those namespaces if the types are used in the source. So you'll need to scan the list of available types to check.
That is why you sometimes see source code with the full class name expanded. e.g. java.util.Date when in import both java.util and java.sql Otherwise the compiler doesn't know which one to use.
Scala has some interesting features about importing from Java packages. You can look at those for inspiration.
If you want to know which classes are in a package you need to list all the class files in the directory for that package in the class path.

Categories

Resources