Studyon Minte9.com
Head First (Java)




Static, Final



* Static variable

	Imagine you wanted to count how many Duck instances are being created while your program is 
	running. How would you do it?

	%java

		// Using a instance varible won't work

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			Dog d1 = new Dog();
			Dog d2 = new Dog();
			System.out.println(d2.dogCount);
			    // Output: 1
		    }
		}

		class Dog {

		    int dogCount = 0; // instance variable

		    public Dog() {
			dogCount++;
		    }
		}

	%java
		
		// Using a static variable is the answer

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			Dog d1 = new Dog();
			Dog d2 = new Dog();
			System.out.println(d2.getCount());
			    // Output: 2
		    }
		}

		class Dog {

		    private static int dogCount = 0; // static variable
                                  // is initialized only once (not each time a new instance is made)

		    public Dog() {
			dogCount++;
		    }
		    public int getCount() {
			return dogCount; // OR this.dogCount
		    }
		}


	%java

		// A static variable can be called without instantiation

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {

			System.out.println(Animal.name);
			    // Output: Weirdo
		    }
		}
		class Animal {
		    public static String name = "Weirdo";
		}


* Static methods

	Take the round() method in the Math class, for example, does the same thing every time-rounds a 
	floating point number (the argument to the method) to the nearest integer. Every time. The method acts
	on the argument, but is never affected by an instance variable state.

	%java
		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			System.out.println(Math.round(42.2)); // 42
			System.out.println(Math.round(33.5)); // 34
		    }
		}


	The Math constructor is private. Math class, for example, doesn't have any instance variables. So 
	there's nothing to be gained by making an instance of class Math. As a matter of fact, you can 't. 

	%java
		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			Math m = new Math();
			    // Error: Math() has private access
		    }
		}


* Static Imports
	
	Non static import

	%java

		package testingapp;
		import java.lang.Math;
			// non-static import

		public class TestingApp {
		    public static void main(String[] args) {
			System.out.println(Math.min(2,3)); // 2
				// Math.min
		    }
		}

	Static import
	
	%java
		
		package testingapp;
		import static java.lang.Math.*;
			// static import (be aware of *)

		public class TestingApp {
		    public static void main(String[] args) {
			System.out.println(min(2,3)); // 2
				// you can use only "min"
		    }
		}

	Use carefully! Static imports can make your code contusing to read.
	If you're only going to use a static member a few times, you should avoid static Imports, to help keep 
	the code more readable.
	Ifyou're going to use a static member a lot, then it's probably OK to use the static Import.


* Static Final

	Static final variables are constants (it can never change)

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {

			Dog d = new Dog();
		    }
		}

		class Dog {
		    public static final int FOO_X = 25;
		    public Dog() {
			System.out.println(FOO_X);
			    // Output: 25
			FOO_X++;
			    // Compile Error
		    }
		}


* Final

	Final isn't just for static variables.

	%java

		// A final variable means you can't change its value

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			Dog d = new Dog();
		    }
		}
		class Dog {

		    public final int x = 25;

		    public Dog() {
			x++; // Compile Error
		    }
		}

	
	%java

		// A final method means you can't override the method

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			Dog d = new Dog();
		    }
		}
		class Animal {    
		    public Animal() {}
		    public final void getName() {}
		}

		class Dog extends Animal {
		    public Dog() {}
		    public void getName() {}
			// Compile Error: Dog override final method
		}

	
	%java

		// A final class means you can't extend the class (you can't make a subclass)

		final class Animal {
		}

		class Dog extends Animal {
		}
			// Compile Error: Can't inherit from final class