Numbers Matter — Chapter 10
312–334
There are no global methods in Java. However static methods are similar to global method in callability.
Eg:
Math.round() //rounds a floating point number to the nearest integer.
Math.min() //takes two numerical primitives and returns the smaller
Math.max() //takes two numerical primitives and returns the smaller
Math.abs() //returns the absolute value of a number.
You simply can’t make instances of Math class.
Math m = new Math(); //will give compiler error (since Math has a private accessor) (Typically if a class has all its membes as static, best approach is to make its constructor private)
The only way to get a new object are by using new or deserialization or something called Java Reflection API.
Referencing between static and non-static methods
Static methods can’t use non-static methods and non-static methods can’t use static methods either. This is because static variables are shared across all the objects in one class.
A static method is good for utilizing a method that does not (and will never) depend on a particular instance variable value.
Initializing a static variable
Static variables are initialized when a class is loaded. (either the JVM or the programmer can decide to load the class).
Static variables in a class are initialized before,
- any object of that class can be created.
- any static method of the class runs.
Also, unless provided, all static variables will hold the default value once the class in initialized.
Static final variables are constants
The value of a static final variable will stay the same (constant) as long as the class is loaded. Constant variable names should be in all cap, with underscores separating words. Eg: Math.PI API
public static final double PI = 3.141592653589793;
static blocks can be used to initialize static variables are they are run as soon as the class is loaded (before any static variable can be used). However, if a value for final static variables is not provided in either of those places (declaration line or static block), the compiler will throw a might not have been initialized error.
Final
final variable — can’t change its value
final method — you can’t override the method. If the class is final, there’s no need to mark the methods final (if a class is final it can never be sub-classed) (none of its methods can ever be overridden)
final class — can’t extend the class (i.e. you can’t make a subclass) (for security reasons) (to make sure a particular implementation of the methods on a class stays the same) Eg: String
Math methods
Math.random — returns a double between 0.0 (not including) and 1.0. double rl = Math. random 0 ;
int r2 = (int) (Math.random() * 5)
Math.abs() — returns a double that is the absolute value of the argument. You can pass it an int and get an int or pass a double and return a double.
int x = Math .abs(-240); //returns 240
double d = Math.abs(240.45); //returns 240.45
//returns 90876.49
Math.round() — returns an int for float and long for double, rounded to the nearest integer value.
int x = Math.round(-24.8f); //returns -25
int y = Math.round(24.45f); //returns 24
Math.min() — returns a value that is the minimum of the two arguments. The method is overloaded to take ints , longs, floats, or doubles.
int x = Math.min(24,240); //returns 24
double y = Math.min(90876.5, 90876 .49);
Math.max() — returns a value that is the maximum of the two arguments. The method is overloaded to take ints, longs, floats, or doubles.
int x = Math.max(24,240); //returns 240
double y = Math.max(90876 .5, 90876.49); //returns 90876.5
Wrapping a primitive
There’s a wrapper class for every primitive type, and since the wrapper classes are in the java. lang package, you don’t need to import them. Mostly used to storing a primitive value inside a collection like an ArrayList or HashMap. However, the autoboxing feature added to Java 5.0 does the conversion from primitive to wrapper object automatically. Autoboxing can also be used in parameter values, return values and operand operations.
Vocabulary
static — a variable or method whose behavior doesn’t depend on an instance variable value.
constants In Java-static final variables