Answer me in one word - 莎士比亚
Objectives:
- How static methods and fields are associated with classes rather than objects.
- How the method-call/return mechanism is supported by the method-call stack.
- About argument promotion and casting.
- How packages group related classes.
- How to use secure random number generation to implement game-playing applications.
- How the visibility of declaration is limited ti specific ragions of programs.
- What method overloading is and how to create overloaded methods.
Outline
6.1 Introduction
6.2 Program Modules in Java
6.3 static Methods, static Fields and Class Math
6.4 Declaring Methods with Multiple Parameters
6.5 Notes on Declaring and Using Methods
6.6 Method-Call Stack and Stack Frames
6.7 Argument Promotion and Casting
6.8 Java API Packages
6.9 Case Study: Secure Random-Number Generation
6.10 Case Study: A Game of Chance; Introducing enum Types
6.11 Scope of Declarations
6.12 Method Overloading
6.13 (Optional) GUI and Graphics Case Study: Colors and Filled Shapes
6.14 Wrap-Up
Hierarchical Relationship Between Method Calls
Hierarchical boss-method/worker-method relationship
Math class methods(static)
static Variables
Recall from Section 3.2 that each object of a class maintains its own copy of every instance variable of the class. There are variables for which each object of a class does not need its own separate copy (as you’ll see momentarily). Such variables are declared static and are also known as class variables. When objects of a class containing static variables are created, all the objects of that class share one copy of those variables. Together a class’s static variables and instance variables are known as its fields. You’ll learn more about static fields in Section 8.11.
Math Class static Constants PI and E
Class Math declares two constants, Math.PI and Math.E, that represent high-precision approximations to commonly used mathematical constants. Math.PI (3.141592653589793) is the ratio of a circle’s circumference to its diameter. Math.E (2.718281828459045) is the base value for natural logarithms (calculated with static Math method log). These constants are declared in class Math with the modifiers public, final and static. Making them public allows you to use them in your own classes. Any field declared with keyword final is constant—its value cannot change after the field is initialized. Making these fields static allows them to be accessed via the class name Math and a dot (.) separator, just as class Math’s methods are.
Why Is Method main Declared static?
When you execute the Java Virtual Machine (JVM) with the java command, the JVM attempts to invoke the main method of the class you specify—at this point no objects of the class have been created. Declaring main as static allows the JVM to invoke main without creating an instance of the class. When you execute your application, you specify its class name as an argument to the java command, as in
java ClassName argument1 argument2 …
The JVM loads the class specified by ClassName and uses that class name to invoke method main. In the preceding command, ClassName is a command-line argument to the JVM that tells it which class to execute. Following the ClassName, you can also specify a list of Strings (separated by spaces) as command-line arguments that the JVM will pass to your application. Such arguments might be used to specify options (e.g., a filename) to run the application. As you’ll learn in Chapter 7, Arrays and ArrayLists, your application can access those command-line arguments and use them to customize the application.
Declaring Methods with Multiple Parameters
Programmer-declared method maximum with three double parameters.
Implementing Method maximum by Reusing Method Math.max
The entire body of our maximum method could also be implemented with two calls to Math.max, as follows:Click here to view code image
return Math.max(x, Math.max(y, z));
The first call to Math.max specifies arguments x and Math.max(y, z). Before any method can be called, its arguments must be evaluated to determine their values. If an argument is a method call, the method call must be performed to determine its return value. So, in the preceding statement, Math.max(y, z) is evaluated to determine the maximum of y and z. Then the result is passed as the second argument to the other call to Math.max, which returns the larger of its two arguments. This is a good example of software reuse—we find the largest of three values by reusing Math.max, which finds the larger of two values. Note how concise this code is compared to lines 30–38 of Fig. 6.3.
Notes on Declaring and Using Methods
There are three ways to call a method:
- Using a method name by itself to call another method of the same class—such as maximum(number1, number2, number3) in line 21 of Fig. 6.3.
- Using a variable that contains a reference to an object, followed by a dot (.) and the method name to call a non-static method of the referenced object—such as the method call in line 16 of Fig. 3.2, myAccount.getName(), which calls a method of class Account from the main method of AccountTest. Non-static methods are typically called instance methods.
- Using the class name and a dot (.) to call a static method of a class—such as Math.sqrt(900.0) in Section 6.3.