Outline - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Outline

Description:

Methods and data-members of a class can be accessed by ... package listing (top left); changes class listing. class listing (bottom left); all classes in ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 23
Provided by: cs11
Category:

less

Transcript and Presenter's Notes

Title: Outline


1
Outline
  • Using classes
  • Use of static methods and data members
  • this keyword
  • Java packages
  • import statements
  • Java class library
  • Built-in classes
  • CLASSPATH

2
Recap
  • Classes and objects
  • Constructors
  • References and null
  • static
  • Class scopes

3
Using Classes
  • public class Print
  • public String name
  • public Print()
  • name new String (default)
  • public Print(String s)
  • name s
  • contd

4
Using classes
  • public void printName()
  • System.out.println(name)
  • public static void main(String arg)
  • Print printObject new Print()
  • printObject.printName()
  • Print printObject1 new Print(First)
  • printObject1.printName()
  • // end of class Print

5
More on classes
  • Methods and data-members of a class can be
    accessed by corresponding objects by using the
    . (dot) operator.
  • To use any method or data-member (non-static) of
    a class, an object has to be instantiated first.
  • Private methods and data-members are not
    accessible to objects
  • A file can contain many classes, but only one of
    these classes will contain the main method. This
    class is the one that shares its name with the
    name of the file.

6
Static methods/Data members
  • public class Print
  • public static String name default
  • public static void printName()
  • System.out.println(name)
  • public static void main(String arg)
  • System.out.println(Print.name) // using
    static data member
  • Print.printName() // using static method.
  • // end of class Print

7
Static methods/Data members
  • A static data-member of a class is common to all
    the objects of that class.
  • To use a static data-member or a static method,
    an object need not be instantiated.
  • A static method cannot use non-static data
    members.

8
this Keyword
  • Refers to the current object
  • i.e., the code in which it appears is in some
    method of some class the current object is the
    instance of that class upon which the method was
    called.
  • Can be used to remove ambiguities between local
    and instance variables this.ltvariablegt is the
    instance variable

9
this Example
  • public class Student
  • private String name
  • public void setName(String name)
  • this.name name

10
Java Packages
  • Classes are organized into packages
  • Basically, a package is a set of related classes
  • Default accessibility is package-wide- only
    classes from the same package can see or use data
    members or methods (or classes!) with no
    accessibility modifier

11
Packages, cont
  • The package a class belongs to is defined by a
    statement at the very top of the filepackage
    ltpackage namegt
  • If no package statement, then it belongs to the
    default nameless package.
  • To use classes in another package, they must be
    imported.

12
Importing Classes
  • Tells the compiler youre going to use classes
    from another package (good software engineering
    practice too)
  • Syntaximport ltpackage namegt.ltclass namegt
  • To import all classes in a packageimport
    ltpackage namegt.
  • Comes before class definition, after package
    statement

13
Example Java Packages
  • Package java.lang
  • Basic classes of the language Integer, etc
  • Automatically imported
  • Package java.io
  • Classes for input and output streams file
    system
  • Package java.util
  • Provides utility classes complex data
    structures, etc

14
More Java Packages
  • Package javax.swing
  • The Swing GUI package cross-platform graphical
    user interfaces
  • Package javax.awt
  • Platform-dependent graphics (abstract windowing
    toolkit)
  • Package java.net
  • Network-oriented classes communication across
    different computers

15
Full Java API Documentation
  • API Application Programmers InterfaceThe set
    of all packages supplied for writing programs in
    Java.
  • Java 2 see web page (Java Class Index)
  • Three frames
  • package listing (top left) changes class listing
  • class listing (bottom left) all classes in
    package changes class details
  • Class details all members of current class

16
Class details
  • Inheritance tree next class
  • Class description English text describing what
    its used for
  • Field summary description of all data members
    that are publicly visible (typically constants)
  • Constructor summary
  • Method summary (including inherited methods)

17
Example - Class String
  • Represents strings of characters (words, etc)
  • Constructors
  • String() - allocates a new blank String
  • String(char ) - construct a string from an
    array of characters
  • String(String) - construct a new string from an
    existing one (duplicate it)

18
A few methods of String
  • boolean equals(Object)
  • compare this String to the specified object
  • boolean equalsIgnoreCase(String)
  • compare this String to the specified String,
    ignoring case (capital vs non-capital letters)
  • String toLowerCase()
  • return a new String that has all the same
    characters except all lower-case
  • int length() - return number of characters

19
CLASSPATH
  • An environment variable that tells the compiler
    where to find imported classes
  • Unnecessary for any of the java. packages
  • A colon-separated list of directories and/or
    files (almost always .jar files)
  • Should usually include . (current directory)

20
Summary
  • Java comes with a large ( 300 classes) class
    library
  • Classes are organized into packages of related
    functionality (only real benefit is easier to
    manage, understand)
  • The Java 2 API describes all of the available
    Java classes
  • For now, most classes you use will be
    injava.lang, java.io, and java.util

21
Next Class
  • Review of material covered so far.
  • MP 3 is due next week.
  • Quiz 2 is next week. Review session time/place to
    be announced on newsgroup.

22
Quiz 2 Material
  • Arrays
  • Basic Object-Oriented Programming
  • Builds on previous material so you should know
    loops, control structures, and so forth as well,
    though that will not be the focus.
Write a Comment
User Comments (0)
About PowerShow.com