Classes 5/5 May 14, 2019 ICS102: Classes 5/5.

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 5 Defining Classes II.
Installing JDK Vijayan Sugumaran Department of DIS Oakland University.
1 Chapter 3 Programs and Packages. 2 Java Virtual Machine (JVM) Java programs execute on the JVM. The JVM is a virtual rather than a physical machine,
Unit2: Object-oriented programming Getting started with Java Jin Sa.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
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
Sadegh Aliakbary Sharif University of Technology Fall 2010.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
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.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
Installing JDK Vijayan Sugumaran Department of DIS Oakland University.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
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.
Building Packages BCIS 3680 Enterprise Programming.
Packages. Access Specifications Public Available anywhere (public keyword) Only one public class per file allowed Protected Available in subclasses, and.
Classes, Interfaces and Packages
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
Working with Packages BCIS 3680 Enterprise Programming.
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.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
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.
Packages In Java.
Chapter 5 Defining Classes II
More About Objects and Methods
Python’s Modules Noah Black.
Chapter 5 Defining Classes II
Packages When we name a program , we name it by class name…
More About Objects and Methods
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Java Basics Packages.
Switch, Rounding Errors, Libraries
Java Packages B.Ramamurthy 11/11/2018 B.Ramamurthy.
CSC240 Computer Science III
Defining Classes II.
Packages Written part of final exam Alex Rudniy
File Handling Programming Guides.
MSIS 655 Advanced Business Applications Programming
Chapter 5 Defining Classes II
Chapter 5 Defining Classes II
More About Objects and Methods
Applying OO Concepts Using Java
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 5 Defining Classes II
Classes, Objects and Methods
CMPE212 – Reminders Assignment 2 due today, 7pm.
Presentation transcript:

Classes 5/5 May 14, 2019 ICS102: Classes 5/5

Outline Packages and Import Statements The Package java.lang Package Names and Directories The Default Package Specifying a Class Path When You Compile May 14, 2019 ICS102: Classes 5/5

- 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 May 14, 2019 ICS102: Classes 5/5

-- 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 May 14, 2019 ICS102: Classes 5/5

-- 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 May 14, 2019 ICS102: Classes 5/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 May 14, 2019 ICS102: Classes 5/5

- 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 May 14, 2019 ICS102: Classes 5/5

-- A Package Name May 14, 2019 ICS102: Classes 5/5

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.statistical.*; import both the utilities.numericstuff and utilities.numericstuff.statistical packages May 14, 2019 ICS102: Classes 5/5

- 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 May 14, 2019 ICS102: Classes 5/5

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 May 14, 2019 ICS102: Classes 5/5

- 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 May 14, 2019 ICS102: Classes 5/5

THE END May 14, 2019 ICS102: Classes 5/5