1. Trang chủ
  2. » Thể loại khác

tài liệu tự học java cho người mới bắt đầu

37 730 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 37
Dung lượng 804 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Class variables Are declared using the static keyword  Once the value is modified, all instances of the class are updated to share the same value... Access Class variables Can also be

Trang 1

Session 6

Module 8: More on Classes Module 9: Exceptions

Trang 2

Module 7 - Review

 Inheritance

 Overloading of methods

Using abstract keyword

Using final keywords

 Interfaces

Trang 3

Module 8 - Objectives

Trang 4

Class variables

Are declared using the static keyword

 Once the value is modified, all instances of the class are updated to share the same value

Trang 5

Access Class variables

 Can also be manipulated without creating an instance of the class

 Are referenced by the class name itself

Trang 6

Static methods

 Also known as class methods, do not have reference to any instance variable in a class, used

to access class variables and methods.

 The keyword this cannot be used inside a static method.

Trang 7

Accessing class method

Trang 8

 The static initialization block

can reference only class

variables that have been

Trang 9

Nested class

 A class defined within another class

 It can have access to members of the outer class (enclosing class) even if the members are declared private

 It can be used for:

 Allow logical grouping of classes

 Increases encapsulation

 More maintainable code

Trang 10

Different type of nested class

Trang 11

Member classes or

non-static nested classes

 It can access all fields and methods of the

outer class, but the reverse is not true

 An outer class cannot access a member of

an inner class even if it is declared as public

because members of an inner class are

declared within the scope of inner class

Trang 12

Local classes

A local class is declared within a method,

constructor or an initializer

In other words, a local class is declared within a

block of code and is visible only within that

particular block

Trang 13

Anonymous classes

 An anonymous class does not have a name It is a type of local class.

Trang 14

Static nested classes

 A static nested class is associated with its outer class

 It cannot refer directly to instance variables or methods defined in its enclosing class, but it can access class

methods or variables directly

 An object needs to be instantiated to access the instance variables and methods of the enclosing class.

Trang 15

Module 9 - Objectives

Introduction to Exceptions

Exception handling in Java

User defined exceptions

Assertions

Trang 16

Introduction to Exceptions

Trang 17

Explain the concept of Exceptions

 An exception are errors that

arises at runtime.

 Exceptions object describles

detail of error.

Trang 18

Causes for Exceptions

 Program errors

 Arise due to errors present in APIs, such as NullPointerException

 Program using these APIs cannot do anything about these errors

 Client code errors

 Arise when client code attempts operations, such as reading content from a file without opening it.

 Exception will provide information about the cause of the error

 Can be handled by client code

 Errors beyond the control of a program

Raised by runtime environment, such as memory error or network

Trang 19

Classification of Exceptions

 Checked Exceptions

 Unchecked

Exceptions

Trang 21

Types of Checked Exceptions

All Checked exceptions are derived from the Exception class.

Trang 22

Unchecked Exceptions

 Generated in situations that are considered non-recoverable for a program (generated during the execution of the program) For example:

 Attempting to access an element beyond the maximum length of

Trang 23

Types of Unchecked Exceptions

All Unchecked exceptions are directly or indirectly derived from the RuntimeException class

Trang 24

Exception handling in Java

Trang 25

The use of try-catch block

Trang 26

The use of multiple catch block

Trang 27

The use of finally block:

Trang 28

The use of throw and throws keyword

Trang 29

The use of throw and throws keyword

exception in a method.

raise any checked or unchecked exception that

it does not handle and enables the caller of the method to guard themselves against exception Except for Error or RuntimeException and their

Trang 30

User defined exception

User defined exception classes are subclassed from the base class Exception These exceptions are handled and thrown in the same manner as predefined exceptions.

Trang 31

Assertions

Trang 32

Using the assert statement

First form

Trang 33

Using the assert statement

Second form

Trang 34

Enabling and Disabling Assertions

Trang 35

Where to use Assertions

Trang 36

Why to use Assertions

Trang 37

Module 8,9 - Summary

Ngày đăng: 22/11/2014, 16:39

TỪ KHÓA LIÊN QUAN

w