click below
click below
Normal Size Small Size show me how
Chapter 4
More Object Concepts
Term | Definition |
---|---|
Block | The code between a pair of curly braces within any class or method |
Re-declaring a Variable | Whey you declare a variable more than once in a block. This is an illegal action |
Variable Overriding | Using a variable's name within the method in which it is declared causes it to take precedence over any other variable with same name in another method, A locally declared variable always masks or hides another variable with same name elsewhere in class |
Shadowing | Is the action that occurs when a local variable hides a variable with the same name that is further away in scope |
1. Overloading | Involves using one term to indicate diverse meanings. In Java, it more specifically means writing multiple methods in the same scope that have the same name but different parameter lists. |
2. Overloading | The names used in the parameter lists do not matter; the lists must differ in parameter type, number of parameters, or both |
Order of Type Promotion | The order of promotion is double, float, long, and int. Any type in this list can be promoted to any type that precedes it |
Ambiguous Situation | A situation in which the compiler cannot determine which method to use. When you overload methods, you risk creating an ambiguous situation |
Reference | An object's memory address |
this Reference | The reference to an object that is passed to any object's nonstatic method. 'this' is a reserved work in Java. Only nonstatic, instance methods have a this reference |
Class Methods | Static method do not have a this reference because they have no object associated with them; therefore, they are called class methods |
Class Variable | Static variables within a class. They are shared by every instantiation of a class |
Nonstatic final Field's Value | A nonstatic final field's value can be set in the class constructor. For example, you can set it using a constant, or you can set it using a parameter passed into the constructor |
Static final Field's Value | A static final field's value must be set at declaration |
1. Static and Final Field Summary | 1. If a want to create a field that all instantiations of the class can access but the field value can change, then it is static but not final... |
2. Static and Final Field Summary | 2. If you want each object created from a class to contains its own final value, you would declare the field to be final but not static... |
3. Static and Final Field Summary | 3. If you want all objects to share a single nonchanging value, then the field is static and final |
Static Methods and this References | Static methods do not have a this reference because they have no object associated with them |
Package(Library of Classes) | A folder that provides a convenient grouping for classes |
java.lang Package | Is implicitly imported into every Java program. The classes it contains are fundamental classes, or basic classes, as opposed to the optional classes that must be name explicitly |
java.lang.Math Class | Contains constants and methods that you can use to perform common mathematical functions. All of the constants and methods in the Math class are static - they are class variables and class methods |
Using Math Class | The java.lang package is imported automatically into your programs, so if you reference Math.PI, Java recognizes this code as a shortcut to a full package path. Ex: areaOfCircle = Math.PI * radius * radius; |
1. Common Math Class Methods | 1. Method[abs(x)]..Value Returned[Absolute value of x]... 2. Method[acos(x)]..Value Returned[Arc cosine of x]... 3. Method[asin(x)]..Value Returned[Arc sine of x]... 4. Method[atan(x)]..Value Returned[Arc tangent of x]... |
2. Common Math Class Methods | 5. Method[atan2(x, y)]..Value Returned[Theta component of the polar coordinate(r, theta) that corresopnds to the Cartesian coordinate x, y]... 6. Method[ceil(x)]..Value Returned[Smallest integral value not less than x(ceiling)]... |
3. Common Math Class Methods | 7. Method[cos(x)]..Value Returned[Cosine of x]... 8. Method[exp(x)]..Value Returned[Exponent, where x is the base of the natural logarithms]... 9. Method[floor(x)]..Value Returned[Largest integral value not greater than x]... |
4. Common Math Class Methods | 10. Method[log(x)]..Value Returned[Natural logarithm of x]... 11. Method[max(x, y)]..Value Returned[Larger of x and y]... 12. Method[min(x, y)]..Value Returned[Smaller of x and y]... 13. Method[pow(x, y)]..Value Returned[x raised to the y power]... |
5. Common Math Class Methods | 14. Method[random()]..Value Returned[Random double number between 0.0 and 1.0]... 15. Method[rint(x)]..Value Returned[Closest integer to x(x is a double, and the return value is expressed as a double)]... |
6. Common Math Class Methods | 16. Method[round(x)]..Value Returned[Closest integer to x(where x is a float or double, and the return value is an int or long)]... 17. Method[sin(x)]..Value Returned[Sine of x]... 18. Method[sqrt(x)]..Value Returned[Square root of x]... |
7. Common Math Class Methods | 19. Method[tan(x)]..Value Returned[Tangent of x] |
Importing Classes | To use any of the other prewritten classes, you must use one of three methods... 1. Use the entire path with the class name.. 2. Import the class.. 3. Import the package that contains the class you are using |
1. Using the GregorianCalendar Class | In its java.util package, Java includes a GregorianCalenndar class that is useful when working with dates and time. You can import java.util.GregorianCalendar; as first line in your program. Then you can instantiate an object of type GregorianCalendar |
2. Using the GregorianCalendar Class | Ex: GregorianCalendar myAnniversary = new GregorianCalendar(); |
1. Wildcard Symbol | An alternative to importing a class is to import an entire package of classes. You can use the asterisk(*) as a wildcard symbol, which indicates that it can be replaced by any set of characters. |
2. Wildcard Symbol | Therefore, the following statement imports the GregorianCalendar class and any other java.util classes as well:... import java.util.*;... The import statement does not move the entire imported class or package into your program, as its name implies. |
3. Wildcard Symbol | Rather, it simply notifies the program that you will use the data and method names that are part of the imported class or package |
1. Some Possible Arguments to and Returns From the GregorianCalendar get() Method | 1. Argument[DAY_OF_YEAR]..Value Returned[A value from 1 to 366]... 2. Argument[DAY_OF_MONTH]..Value Returned[A value from 1 to 31]... |
2. Some Possible Arguments to and Returns From the GregorianCalendar get() Method | 3. Argument[DAY_OF_WEEK]..Value Returned[SUNDAY, MONDAY, ...SATURDAY, corresponding to values from 1 to 7]... 4. Argument[YEAR]..Value Returned[The current year; for example, 2012] |
3. Some Possible Arguments to and Returns From the GregorianCalendar get() Method | 5. Argument[MONTH]..Value Returned[JANUARY, FEBRUARY, ...DECEMBER, corresponding to values from 0 to 11].. 6. Argument[HOUR]..Value Returned[A value from 1 to 12; the current hour in the A.M. or P.M.]... |
4. Some Possible Arguments to and Returns From the GregorianCalendar get() Method | 6. Argument[AM_PM]..Value Returned[A.M. or P.M., which correspond to values from 0 to 1]... 7. Argument[HOUR_OF_DAY]..Value Returned[A value from 0 to 23 based on 24-hour clock]... |
5. Some Possible Arguments to and Returns From the GregorianCalendar get() Method | 8. Argument[MINUTE]..Value Returned[The minute in the hour, a value from 0 to 59]... 9. Argument[SECOND]..Value Returned[The second in the minute, a value from 0 to 59]. |
6. Some Possible Arguments to and Returns From the GregorianCalendar get() Method | Ex:... nowYear = now.get(GregorianCalendar.YEAR); |
Composition | Describes the relationship between classes when an object of one class is a data field within another class |
Nested Classes | In Java, you can create a class within another class and store them together; such classes are nested classes. The containing class is the top-level class. |
Four Types of Nest Classes | There are four types of nested classes... 1. static member classes. 2. Nonstatic member classes.. 3. Local classes.. 4. Anonymous classes |
static Member Classes | A static member class has access to all static methods of the top-level class |
Nonstatic Member Classes | Also known as inner classes: This type of class requires instance; has access to all data and methods of top-level class. Inner class can access top-level class's fields and methods, even if private, and outer class can access its inner class's members |
Local Classes | These are local to a block of code |
Anonymous Classes | These are local classes that have no identifier |