Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

 Static methods are called functions in many programming languages.

 They are a sequence of statements that are executed one after the
other when called.
 They are distinguished from instance methods by the modifier
'static'.
 A method encapsulates a computation defined as a sequence of
statements.
 A method takes arguments and computes a return value or causes a
side effect.
 A static method is composed of a signature (keywords, return type,
method name, arguments) and a body (statement block).
 To invoke a static method, its name is followed by expressions
specifying argument values in parentheses.
 When a method is called, its argument variables are initialized with
the values of the corresponding expressions in the call.
 A return statement terminates a static method and returns control
to the caller.
 If a static method can reach the end of its sequence of statements
without a return statement, the compiler will report an error.
 Methods have arguments that are passed by value, meaning that the
method works with the value of the arguments, not the arguments
themselves. This means that changing the value of an argument
variable within a method will not affect the calling code.
 Method names can be overloaded, meaning that multiple versions of
a method can have the same name but different parameters.
 A method has a single return value, but can have multiple return
statements. Control goes back to the calling program as soon as the
first return statement is reached.
 A method can have side effects, meaning that it can change the state
of the system without returning a value. This is indicated by using
the keyword "void" as the return type.
 Instance methods have the same properties as static methods, but
have additional differences surrounding the issue of side effects.
 A method can have default values for its arguments, which means
that the caller can choose to use the default values or provide their
own values when calling the method.
 A method can have access modifiers, such as public or private, which
determine the scope of the method and who can access it.
 A method can throw exceptions, which are errors that occur during
the execution of the method. The method can handle the exception
or the caller can handle it.
 A method can be overridden, meaning that a subclass can provide its
own implementation of a method that is already defined in its
superclass.
 Methods can be recursive, meaning that a method can call itself in
order to solve a problem or complete a task.
 Methods can be used to encapsulate logic and functionality, making
the code more organized and easier to maintain.
 Recursion is a method where a function can call itself
 It can lead to compact and elegant code that is easy to understand
 There are three important rules to follow when developing recursive
programs: a base case, addressing smaller subproblems, and
avoiding overlapping subproblems
 Adhering to these guidelines can lead to clear and correct programs
with easy to understand performance
 Recursive methods can also lead to mathematical models that can be
used to understand performance.
 A basic programming model in Java involves creating a library of
static methods in a class file with the same name as the class and
a .java extension, with one of the methods being named main().
 This model allows for modular programming, where libraries of static
methods can be used and called upon by other libraries.
 This approach has many advantages, such as the ability to work with
smaller modules, share and reuse code, easily substitute improved
implementations, and develop appropriate abstract models for
addressing programming problems.
 A best practice in Java programming is to include a main() method in
every library of static methods for unit testing.
 As a module matures, the main() method can be refined to be a
development or test client, and may even be put in an independent
module.
 In this book, main() is used to help illustrate the purpose of each
module and test clients are left for exercises.
 The author discusses the use of external libraries in the book,
including:
 Standard system libraries such as java.lang.*, which includes Math,
Integer, Double, String, and StringBuilder.
 Imported system libraries such as java.util.Arrays
 Other libraries developed by the author for use in the book, such as
the Std* libraries.
 To use methods from these libraries, the author advises prepending
the library name to the method name for each call.
 Libraries can greatly expand the scope of programming and
thousands more are available online. However, the author limits the
scope of the programming model in the book to a manageable size
by focusing on a specific set of libraries.
 APIs are a critical component of modular programming and provide
documentation for library methods intended for use by others.
 The book uses Java libraries, as well as their own standard libraries
for input and output, and for testing and analyzing implementations.
 The initialize() method in StdRandom allows for reproducing
experiments involving random numbers.
 Well-designed libraries provide a level of abstraction, test for
exceptional conditions, and are extensively tested for reliability.
 It is recommended to study the code in StdRandom.java and
StdStats.java on the booksite and to take advantage of these tried-
and-true implementations.
 The author suggests considering every program written as a library
implementation for possible reuse in the future.
 The purpose of an API is to separate the client from the
implementation, allowing the client to know nothing about the
implementation other than information given in the API.
 It is important to develop clear and comprehensive APIs for libraries
in order to make them easy to use and understand for other
programmers.
 Downloading the source code from the booksite and putting it in
your working directory is the easiest way to use these libraries and
examine the code.
 Writing code as a library implementation and developing an API for
it can provide valuable software for future reuse and is a key to
successfully addressing complex programming tasks.
 Modular programming with APIs allows for a clear separation of
concerns and makes it easy to maintain and update code.
 The use of standard libraries and well-designed APIs can save time
and effort in the development process and increase code reliability.

You might also like