final: Variables, Methods and Classes
Sometimes we don’t want child classes to override the implementation in the parent!
Java keyword final. – Using this keyword will prevent child classes (or anyone else) modifying the variable/method this applies to.We can prevent a class from being inherited at all!
Suppose we want to keep track of how many instances of a class are made. – Question: How can we automatically keep track? • cannot leave it to the objects; • cannot use an instance variable. – Answer: Use a class variable. • A class variable is created when the class is created, rather than when an object is created. – There may be n instances of the class, but there will only be one instance of the class variable. – To declare a class variable, use the modifier static.
Normally every instance of an object has its own copy of all variables and methods defined in the class.If you declare something as static, it means that all objects have the same copy of that variable/method.
Static methods can only reference static variables!
Static is like global variables but applies classwide. • Static methods become available when the class is loaded (created), not when you make an instance of it
Can access static methods even when no instance of a class has been created!
Math Class and Methods
Methods of class Math: almost like global methods! – Act on the argument but are not affected by an instance variable state.
Example: int x = Math.min(56,12); always does the same thing and doesn’t use instance variables. Method’s behaviour doesn’t need to know about a given object.
Math Class: – Doesn’t have instance variables. – Can’t make an instance of class.
For static method ,it is no sense to use them as instance method
Classes that can’t be instantiated: – Abstract classes and interfaces. – Classes with private constructors. • Only code inside the class can invoke a private constructor! • Methods in class Math are static.
– Invoking a Math method:Math.max(10,20);//calling a static method: use the class name
A non-initialised static variable will have the default value of that variable type.
naming convention for static final variables: all caps
static block to initialise static final variables
final class: cannot extend the class
final classes and abstract classes: not the same!
Math.random(): returns a double in range 0.0-1.0 (excluding).
• Math.abs(double num): returns a double that is the absolute value of num; method is overloaded to take/return an int value.
• Math.round(float value): returns a value rounded to the nearest int value; method is overloaded to take a double value and return a long value.
• Math.min(int a, int b): returns an int that is the minimum value between a and b; method is overloaded to take/return a long, float or double value.
• Math.max(int a, int b): returns an int that is the maximum value between a and b; method is overloaded to take/return a long, float or double value.
Wrapping Classes: used when a variable of a primitive type needs to be treated as an object. – Every primitive type has a wrapper class. – Wrapper classes are part of java.lang package no need to import them
Autoboxing: automatic wrapping conversion from primitive type to wrapper object is automatic!
Integer i = new Integer(10); i++; System.out.println(“New value is: “ + i);
Integer d = x;int x = 10;Integer x = 10;
Wrappers have parse methods: they take a String and return a primitive value.
String str1 = “10”; String str2 = “123.45”; String str3 = “true”; int i = Integer.parseInt(str1); // i=10 double d = Double.parseDouble(str2); // d=123.45 boolean b = new Boolean(str3).booleanValue(); // b=true
All wrapper classes are subclasses of the Number abstract class (part of the java.lang package).
Number num = new Integer(10);
Subclasses of Number provide constants to, e.g. represent the upper and lower bounds of the corresponding data type (MIN_VALUE and MAX_VALUE, respectively). • Example: System.out.println(Long.MIN_VALUE); –263
Methods that call themselves, directly or indirectly, are known as recursive
Extra memory required + time to manage the extra space.
• use an iterative approach if that is the obvious solution; • avoid using recursion if there are concerns about the program’s performance.