Chapter 2.6.4 – The Problem of Packages | Introduction to Programming Using Java
2.6.4 The Problem of Packages
Every class in Java is contained in something called a package. Classes that are not explicitly put into a different package are in the “default” package. Almost all the examples in this textbook are in the default package, and I will not even discuss packages in any depth until Section 4.5. However, some IDEs might force you to pay attention to packages.
When you create a class in Eclipse, you might notice a message that says that “The use of the default package is discouraged.” Although this is true, I have chosen to use it anyway, since it seems easier for beginning programmers to avoid the whole issue of packages, at least at first. Some IDEs might be even less willing than Eclipse to use the default package.
If you create a class in a package, the source code starts with a line that specifies which package the class is in. For example, if the class is in a package named testpkg, then the first line of the source code will be package testpkg;
In an IDE, this will not cause any problem unless the program you are writing depends on TextIO. You will not be able to use TextIO in a program unless TextIO is placed into the same package as the program.
This means that you have to modify the source code file TextIO.java to specify the package; just add a package statement using the same package name as the program. Then add the modified TextIO.java to the same folder that contains the program source code. Once you’ve done this, the example should run in the same way as if it were in the default package.
By the way, if you use packages in a command-line environment, other complications arise. For example, if a class is in a package named testpkg, then the source code file must be in a subdirectory named testpkg that is inside your main Java working directory. Nevertheless, when you compile or execute the program, you should be in the main directory, not in the subdirectory.
When you compile the source code file, you have to include the name of the directory in the command: Use “javac testpkg/ClassName.java” on Linux or Mac OS, or “javac testpkg\ClassName.java” on Windows. The command for executing the program is then “java testpkg.ClassName”, with a period separating the package name from the class name. Since packages can contain subpackages, it can get even worse than this!
However, you will not need to worry about any of that when using the examples in this book.
Read More…
Introduction to Programming Using Java – David J. Eck
Chapter 2 – Names and Things | Introduction to Programming Using Java
Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java
Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java
Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java
Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java
Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java
1 thought on “Chapter 2.6.4 – The Problem of Packages | Introduction to Programming Using Java”