Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.

Slides:



Advertisements
Similar presentations
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
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.
15-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
Style Rules II: Names and Naming. Overview Today we will discuss: –Reasons for naming conventions –Rules for naming variables, classes, and methods.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
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.
Agile Lab 2008a Armin B. Cremers, Günter Kniesel, Pascal Bihler, Tobias Rho, Marc Schmatz, Daniel Speicher Sommersemester 2008 R O O T S Coding standards.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
Packages F Package is a container for classes F A package is a grouping of related types (classes and interfaces) providing access protection and name.
1 Lecture 2: Object Oriented Programming in Java.
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
Object Oriented Programming in Java Habib Rostami Lecture 5.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.
EE2E1. JAVA Programming Lecture 3 Java Programs and Packages.
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.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Code Conventions Tonga Institute of Higher Education.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Packages. Creating and Using Packages To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
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.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
A variable is a name for a value stored in memory.
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
Software Construction
University of Central Florida COP 3330 Object Oriented Programming
Java Basics Packages.
Packages Written part of final exam Alex Rudniy
Packages and Interfaces
Package & Java Access Specifiers
CISC124 Assignment 3 due tomorrow at 7pm.
Applying OO Concepts Using Java
Anatomy of a Java Program
Tonga Institute of Higher Education
Variables and Constants
Presentation transcript:

Even More Java

Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. (Java’s Definition)

Java Packages Access Protection means that using a package, as I showed last time, we can use different interfacing which can limit usage to within the package Name space management essentially means controlling scope. For example there is already a defined Vector class. There is no name conflict with your own because its in the Java.util package

Creating a Package Choose a name for your package Every source file must have package as the first line of the file. Make sure you follow naming conventions on the next slide

Package Naming Conventions Packages have a naming convention The name is lower case so it isn’t confused with a type or interface Often companies use the reverse of their domain name, ie com.example.orion All provided packages start with java. Or javax. Some cases the plain internet address cannot be used, usually we add an underscore; ie clipart-open.org -> org.clipart_open, free.fonts.int -> int_.fonts.free, poetry.7days.com -> com._7days.poetry

Using Package Members There are 3 ways to access a public package member from outside the package Use the full qualified name, ie Java.util.Vector v = new Java.util.Vector() Import the package member, ie import Java.util.Vector; Vector v = new Vector() Import the package, ie import Java.util.*; Vector v = new Vector() The last option is usually discouraged for using a single class.

Apparent Hierarchies Sometimes it appears that some classes are contained in a package when in truth they aren’t; ie import java.awt.* will not import java.awt.color Therefore always look at your API

Name Ambiguities Should you ever import a package with an identical class name you must then qualify you classes to avoid ambiquity. For example, if on your last assignment you had imported java.util.Vector, every time you declared a vector you would have to qualify. Vector v = new Vector() is now ambiguous.

Static Import Statement If you want to import the static methods and fields of a class you can do this. For example, java.lang.Math contains a static PI field. If you wanted to import this simply type import static java.lang.Math.PI or as a group import static java.lang.Math.* Overusing static imports tends to make your code unreadable so use them sparingly

Managing Files When using packages, the class graphics.Rectangle should be in the directory /graphics/Rectangle Both your.java and.class files should be in this directory structure, but they don’t have to be the same one You can change your classpath

ClassPath Both the compiler and the JVM use your classpath The compiler will create directories based on packages for you You can change your classpath easily To display the current CLASSPATH variable, In Windows: C:\> set CLASSPATH In Unix: % echo $CLASSPATH To delete the current contents of the CLASSPATH variable In Windows: C:\> set CLASSPATH= In Unix: % unset CLASSPATH; export CLASSPATH To set the CLASSPATH variable, In Windows: C:\> set CLASSPATH=C:\users\george\java\classes In Unix: % CLASSPATH=/home/george/java/classes; export CLASSPATH

Class Naming Conventions Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words- avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

Variable Naming Conventions Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. ie., float length, int height Some conventions say all private variables should start with an underscore, others don’t. ie., private float _lentgth; private int _height

Other Naming Conventions Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. ie., run(), runFast(), getBackground() Constants should be declared in all capitals seperated by underscores. ie., static final int MIN_WIDTH = 4; static final int MAX_WIDTH = 999;