Studyon Minte9.com
Head First (Java)




Variables



		// parameters (type matches needed)
		// method's variables are pass-by-value (copy)
		// set/get (java standard)
		// instance variables (always default value
		// local variables (must be initialized before used)


* Type matches
	
	You can pass variables into a Method, as long as the variable type matches the parameter type.

	%java

		public class TestingApp {
		    
		    public static void main(String[] args) {
			
			Dog d = new Dog();
			int x = 3;
			String y = 'a';
			d.test(x, y);            
		    }
		}

		class Dog {
		    public void test(int z, int b) {
		    }
		}


* Pass-by-value

	Variables in Java are pass-by-value (it means by copy)

	%java

		public class TestingApp {
		    
		    public static void main(String[] args) {
			
			Dog d = new Dog();
			int x = 3;
			d.test(x);
			System.out.println(x);
			    // Output: 3 (not 0)
			    // Variable is pass-by-value
			    
		    }
		}

		class Dog {
		    public void test(int z) {
		       z = 0; 
		    }
		}


* Java standard (set/get)

	Using these naming conventions means you'll be following an important Java standard

	%java

		public class TestingApp {
		    
		    public static void main(String[] args) {
			
			Dog d = new Dog();  
			d.setName("Foo");
			System.out.println(d.getName());
			    // Output: Foo
		    }
		}

		class Dog {
		    String dogName;
		    
		    void setName(String x) {
			dogName = x;
		    }
		    String getName() {
			return dogName;
		    }
		}


* Instance variables

	Instance variables always get a default value. If you don't explicitly assign a value to an instance 
	variable, or you don't call a setter method, the instance variable still has a value!

	%java

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

		class Dog {
		    
		    int a;
		    float b;
		    boolean c;
		    Dog d;
		    
		    void test() {
			System.out.println(a);
			    // 0
			System.out.println(b);
			    // 0.0
			System.out.println(c);
			    // false
			System.out.println(d);
			    // null
		    }
		}


* Local variables

	Local variables are declared within a method. Local variables MUST be initialized before use!

	%java

	    public static void main(String[] args) {
		
		int x;
		int z = x+1; // Compile Error
	    }