



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of java program organization, focusing on the use of packages and class access. It covers the concept of packages as collections of related classes and interfaces, their hierarchical organization, and access to package members. The document also includes examples of package usage and file structure.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




javax.swing: classes dealing with the development of GUIs. java.lang: essential classes required by the Java language. java.text: facilities for formatting text output. java.util: classes for storing/accessing collections of objects. java.net: for network communication.
java.awt: classes for basic GUI elements and graphics. java.awt.font: classes and interface relating to fonts. java.awt.geom: classes for defining 2-dimensional objects. There is no limit to the nesting depth.
Accessing Package Members: Fully qualified name: E.g., javax.swing.JOptionPane Importing a single class: import javax.swing.JOptionPane; … JOptionPane.showMessageDialog( … ); Importing all the classes: import javax.swing.*; … JOptionPane.showMessageDialog( … ); Import semantics: import does not “insert” the Java files (as C/C++ do with “include” files). Instead, it tells the compiler where to look to find classes that the program refers to. Multiple import statements: You can have as many as you like. They go at the top of your .java file (before any classes or interfaces). java.lang: is automatically imported into every program.
package graphics.shapes; public class Circle { private double radius; public String toString( ) { return "I'm a circle"; } }
package graphics.shapes; public class Rectangle { private double height, width; public String toString( ) { return "I'm a rectangle"; } }
package graphics.shapes; public class OtherShape { private Circle c; // Can access other classes in this package directly private Rectangle r; }
package graphics.otherstuff; public class PublicClass1 { // A public class public String toString( ) { return "This is a PublicClass: " + NonPublicClass1.message( ); } } class NonPublicClass1 { // A nonpublic class: Only accessible in the package static public String message( ) { return "I'm a nonpublic class"; } }
package graphics.otherstuff; public class PublicClass2 { private Driver d; // NO! We have no direct access to parent package private Circle c1; // NO! We have no direct access to sister package private graphics.shapes.Circle c2; // Okay. Can access public classes elsewhere public String toString( ) { return "This is a PublicClass2: " + NonPublicClass1.message( ); } }
package graphics; import graphics.shapes.Circle; public class Driver { public static void main(String[] args) { testShapes( ); testOtherStuff( ); } public static void testShapes( ) { Circle c = new Circle( ); System.out.println( c.toString( ) ); Rectangle r = new Rectangle( ); // NO! Cannot access without import } public static void testOtherStuff( ) { PublicClass1 x = new PublicClass1( ); // NO! Cannot access without import graphics.otherstuff.PublicClass1 y = new graphics.otherstuff.PublicClass1( ); System.out.println( y ); graphics.otherstuff.NonPublicClass1 z; // NO! not visible here } }