Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.

Slides:



Advertisements
Similar presentations
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Advertisements

Chapter 5 Defining Classes II Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
1 CSE 331 Java Packages; JAR Archives slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Week1 Using the Library (the Java API) Classes are grouped together in packages –To use a class you have to know which package it is in –Every package.
Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
22-July-2002cse142-13B-Development © 2002 University of Washington1 Development Tools CSE 142, Summer 2002 Computer Programming 1
CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Packages. Access Specifications Public Available anywhere (public keyword) Only one public class per file allowed Protected Available in subclasses, and.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Static Methods Slides 2 to 15 only Static Methods A static method is one that can be used without a calling object A static method still belongs.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax.
Chapter 5 Defining Classes II
More About Objects and Methods
Chapter 5 Defining Classes II
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Java Basics Packages.
Chapter 5 Defining Classes II
Chapter 5 Defining Classes II
More About Objects and Methods
Applying OO Concepts Using Java
Chapter 5 Defining Classes II
Packages & Random and Math Classes
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II

© 2006 Pearson Addison-Wesley. All rights reserved5-2 Packages and Import Statements Java uses packages to form libraries of classes A package is a group of classes that have been placed in a directory or folder, and that can be used in any program that includes an import statement that names the package –The import statement must be located at the beginning of the program file: Only blank lines, comments, and package statements may precede it –The program can be in a different directory from the package

© 2006 Pearson Addison-Wesley. All rights reserved5-3 Import Statements We have already used import statements to include some predefined packages in Java, such as Scanner from the java.util package import java.util.Scanner; It is possible to make all the classes in a package available instead of just one class: import java.util.*; –Note that there is no additional overhead for importing the entire package

© 2006 Pearson Addison-Wesley. All rights reserved5-4 The package Statement To make a package, group all the classes together into a single directory (folder), and add the following package statement to the beginning of each class file: package package_name; –Only the.class files must be in the directory or folder, the.java files are optional –Only blank lines and comments may precede the package statement –If there are both import and package statements, the package statement must precede any import statements

© 2006 Pearson Addison-Wesley. All rights reserved5-5 The Package java.lang The package java.lang contains the classes that are fundamental to Java programming –It is imported automatically, so no import statement is needed –Classes made available by java.lang include Math, String, and the wrapper classes

© 2006 Pearson Addison-Wesley. All rights reserved5-6 Package Names and Directories A package name is the path name for the directory or subdirectories that contain the package classes Java needs two things to find the directory for a package: the name of the package and the value of the CLASSPATH variable –The CLASSPATH environment variable is similar to the PATH variable, and is set in the same way for a given operating system –The CLASSPATH variable is set equal to the list of directories (including the current directory, ". ") in which Java will look for packages on a particular computer –Java searches this list of directories in order, and uses the first directory on the list in which the package is found

© 2006 Pearson Addison-Wesley. All rights reserved5-7 A Package Name

© 2006 Pearson Addison-Wesley. All rights reserved5-8 Pitfall: Subdirectories Are Not Automatically Imported When a package is stored in a subdirectory of the directory containing another package, importing the enclosing package does not import the subdirectory package The import statement: import utilities.numericstuff.*; imports the utilities.numericstuff package only The import statements: import utilities.numericstuff.*; import utilities.numericstuff.statistical.*; import both the utilities.numericstuff and utilities.numericstuff.statistical packages

© 2006 Pearson Addison-Wesley. All rights reserved5-9 The Default Package All the classes in the current directory belong to an unnamed package called the default package As long as the current directory (. ) is part of the CLASSPATH variable, all the classes in the default package are automatically available to a program

© 2006 Pearson Addison-Wesley. All rights reserved5-10 Pitfall: Not Including the Current Directory in Your Class Path If the CLASSPATH variable is set, the current directory must be included as one of the alternatives –Otherwise, Java may not even be able to find the.class files for the program itself If the CLASSPATH variable is not set, then all the class files for a program must be put in the current directory

© 2006 Pearson Addison-Wesley. All rights reserved5-11 Specifying a Class Path When You Compile The class path can be manually specified when a class is compiled –Just add –classpath followed by the desired class path –This will compile the class, overriding any previous CLASSPATH setting You should use the –classpath option again when the class is run