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 1Session 6
Module 8: More on Classes Module 9: Exceptions
Trang 2Module 7 - Review
Inheritance
Overloading of methods
Using abstract keyword
Using final keywords
Interfaces
Trang 3Module 8 - Objectives
Trang 4Class 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 5Access Class variables
Can also be manipulated without creating an instance of the class
Are referenced by the class name itself
Trang 6Static 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 7Accessing class method
Trang 8 The static initialization block
can reference only class
variables that have been
Trang 9Nested 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 10Different type of nested class
Trang 11Member 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 12Local 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 13Anonymous classes
An anonymous class does not have a name It is a type of local class.
Trang 14Static 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 15Module 9 - Objectives
Introduction to Exceptions
Exception handling in Java
User defined exceptions
Assertions
Trang 16Introduction to Exceptions
Trang 17Explain the concept of Exceptions
An exception are errors that
arises at runtime.
Exceptions object describles
detail of error.
Trang 18Causes 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 19Classification of Exceptions
Checked Exceptions
Unchecked
Exceptions
Trang 21Types of Checked Exceptions
All Checked exceptions are derived from the Exception class.
Trang 22Unchecked 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 23Types of Unchecked Exceptions
All Unchecked exceptions are directly or indirectly derived from the RuntimeException class
Trang 24Exception handling in Java
Trang 25The use of try-catch block
Trang 26The use of multiple catch block
Trang 27The use of finally block:
Trang 28The use of throw and throws keyword
Trang 29The 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 30User 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 31Assertions
Trang 32Using the assert statement
First form
Trang 33Using the assert statement
Second form
Trang 34Enabling and Disabling Assertions
Trang 35Where to use Assertions
Trang 36Why to use Assertions
Trang 37Module 8,9 - Summary