Java Packages and Import Statements
The GUI version of TextIO is textiogui/TextIO.java, a class that opens a window where TextIO I/O operations are performed.
Two Classes Sharing One Name
Many introductory Java programs read text from the user and print text back. Normally that input and output happens on the command line. The GUI version of TextIO takes the same kind of text-oriented work and places it in a window instead. The source material for this article describes that version, how its pieces fit together, and why it lives in its own package.
What do you think happens?
The GUI version and the normal version are both named TextIO. If a program mentions TextIO, what decides which of the two classes it gets?
Reveal answer
The package name decides it. The GUI version sits in the textiogui package, so the package name tells you which TextIO you are using.
Both classes share the name TextIO, so the name alone cannot distinguish them. The package keeps them apart, and an import statement that includes the package name selects the GUI version.
What the GUI Version Changes
The GUI version of TextIO is the file textiogui/TextIO.java. It is a class that opens a window where TextIO input and output operations are performed. The normal TextIO handles the same kind of text-oriented work, but its input and output happens on the command line. The only difference the source states is where the text appears: the GUI version moves that work into a window.
The source describes the GUI version's purpose but does not walk through the method calls or internal steps the window uses. This article therefore stops at the level the source supports: the GUI version performs TextIO input and output in a window. It does not explain how each individual operation is carried out inside that window.
Inside the textiogui Package
The GUI version lives in a package named textiogui. Think of the package as a container with two members, where one member does the main work and the other supports it. The package holds two source files: textiogui/TextIO.java and textiogui/System.java.
Why the Package Name Decides the Class
The package name is not arbitrary. The source states that the package exists to distinguish the GUI version from the normal TextIO. Both classes share the name TextIO, so placing the GUI version in textiogui keeps the two apart. When you work with the GUI version, the package name tells you which TextIO you are using.
An import statement that includes the package name selects the GUI version and not the normal class. The source does not include code, so the snippet below is an illustration written for this article. The source also does not name the methods TextIO provides, so the body of the class is left as a placeholder.
- The import line names the package and the class: textiogui is the package, TextIO is the class.
- Because the package name is included, the compiler resolves TextIO to the GUI version.
- The main method is a placeholder; the source does not name TextIO methods to call.
Tracing which TextIO a program uses
A program contains the import line import textiogui.TextIO; and then refers to TextIO in its code. Which class does that name stand for?
- 1Read the import line
The line names a package, textiogui, and a class, TextIO, joined by a dot.
- 2Check which package is named
The package named is textiogui, the package that holds the GUI version.
- 3Apply the package name
The package name tells you which TextIO is in use, so the name TextIO refers to textiogui.TextIO.
- 4Conclude
The program uses the GUI version, the class that opens a window for TextIO input and output.
The program uses the GUI version of TextIO, not the normal class.
Mistakes When Choosing a TextIO
Common mistake Assuming the class name alone identifies the GUI version
Why it failsBoth classes are named TextIO, so the name by itself does not say which one is in use.
Do this insteadLook at the package name. The textiogui package name identifies the GUI version.
Common mistake Expecting the GUI version's output to appear on the command line
Why it failsThe GUI version performs TextIO input and output in a window, not on the command line.
Do this insteadExpect text to appear in the window that the GUI version opens.
Common mistake Expecting System.out output to ignore the window
Why it failsWithout the companion class,
System.outoutput would not be directed into the GUI window. The companion System class is what routes it there.Do this insteadWhen using the GUI version, expect System.out output to appear in the same window as TextIO output.
When the GUI Version Fits
The source says the author uses these classes to build executable jar files for text-oriented examples that run in a window instead of on the command line. So the typical purpose is not a single standalone class but a complete program packaged as a jar. Each text-oriented example can run by double-clicking its jar rather than by typing commands in a terminal.
A reasonable reading of the source is that the GUI version fits a text-oriented program whose input and output should happen in a window, for example a program meant to be launched by double-clicking. A program that should keep its text on the command line is a case for the normal TextIO. The source does not give a full decision procedure, so this rule of thumb is a reading of its stated purpose.
Practice Choosing the Right Version
A teacher wants a text-oriented example that students can start by double-clicking, with its text shown in a window rather than typed into a terminal. Which TextIO should the example import, and what line of the program would make that choice? Explain your answer using the package name.
Hints
- Think about where the text should appear: a window or the command line.
- The package name in the import line is what distinguishes the two classes.
Key Takeaways
- The GUI version of TextIO is textiogui/TextIO.java, a class that opens a window where TextIO input and output operations are performed.
- The textiogui package holds two classes: TextIO, the GUI version that does the main work, and System, a companion class described as a fake System class that lets
System.outoutput appear in the same window. - Both the GUI version and the normal TextIO share the name TextIO, so the textiogui package name is what tells you which one a program is using.
- The GUI version fits text-oriented programs whose input and output should happen in a window, such as executable jar examples launched by double-clicking.
- The source does not describe the internal method calls or window steps, so detailed behavior beyond these roles is outside what it establishes.