Chapter 2 Classes and Objects

1) 
Body mercury;

This declaration states that mercury is a variable that can hold a reference to an object of type Body. The declaration DOES NOT create an object.

2) this(...) could be used to call constructors:

class Body {
    public long idNum;
    public String name;
    public Body orbits;
    Body() {
        idNum = nextID++;
    }
  
    Body(String bodyName, Body orbitsAround) {
        this(); // explicit constructor invocation, call constructor Body()
        name = bodyName;
        orbits = orbitsAround;
    }
public static long nextID = 0;}

3)Initialization Block: a block of statements that appears within the class declaration, outside of any member, or constructor, declaration and that initializes the fields of the object.

class Body {
    public long idNum;
    public String name = "<unnamed>";
    public Body orbits = null;

    private static long nextID = 0;

    {
        idNum = nextID++;
    }

    public Body(String bodyName, Body orbitsAround) {
        name = bodyName;
        orbits = orbitsAround;
    }
}


The initialization block is executed as if it were placed at the beginning of every constructor in the class - with multiple blocks being executed in the order they appear in the class.

Code:

public class InitializationBlock {
	{
		System.out.println("Block 1");
	}
	
	{
		System.out.println("Block 2");
	}
	
	{
		System.out.println("Block 3");
	}
	
	InitializationBlock(){
		System.out.println("Default Constructor!");
	}
	
	InitializationBlock(String str) {
		System.out.println("Constructor with parameter!");
	}
	
	public static void main(String[] args) {
		InitializationBlock block1 = new InitializationBlock();
		InitializationBlock block2 = new InitializationBlock("JavaBeta");
	}
}

Output:
Block 1
Block 2
Block 3
Default Constructor!
Block 1
Block 2
Block 3
Constructor with parameter!
A static initialization block is much like a non-static initialization block except it is declaredstatic, can only refer to static members of the class, and cannot throw any checked exceptions.


class Primes {
    static int[] knownPrimes = new int[4];

    static {
        knownPrimes[0] = 2;
        for (int i = 1; i < knownPrimes.length; i++)
            knownPrimes[i] = nextPrime();
    }
    // declaration of nextPrime ...
}
4) Methods with Variable Numbers of Arguments
The last parameter in a method (or constructor) parameter list can be declared as a sequence of a given type. To indicate that a parameter is a sequence you write an ellipse (...) after the parameter type, but before its name.
public static void print(String... messages) {
    // ...
}

Sequence parameters allow a method to be invoked with a variable number of arguments (including zero) that form the sequence.
System.out.printf is an example of method with variable numbers of arguments
5) All objects have a toString method whether their class explicitly defines one or not - this is because all classes extend the class Object and it defines the toString method.
6)All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments. If you pass adouble to a method, its parameter is a copy of whatever value was being passed as an argument, and the method can change its parameter's value without affecting values in the code that invoked the method. 
You should note that when the parameter is an object reference, it is the objectreferencenot the object itselfthat is passed "by value." Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it.
Code:

class Person{
	String name;
	int age;
	Person(String name, int age){this.name = name; this.age = age;}
	void print(){System.out.println("(" + name + "," + age + ")");}
}
public class PassByValue {
	static void change(Person person){
		person.name = "JavaBeta";
		person.age = 27;		
	}
	
	static void modify(Person person){
		person = new Person("JavaBeta", 27);		
	}
	public static void main(String[] string) {
		Person person = new Person("JavaAlpha", 27);
		person.print();
//		change(person); //change the fields of person
		modify(person); //pass-by-value, so the object refernce is unchanged!
		person.print();
	}
}

Output:
(JavaAlpha,27)
(JavaBeta,27)


The Java programming language DOES NOT pass objects by reference; All parameters to methods are passed "by value".


7)You can declare method parameters to be final, meaning that the value of the parameter will not change while the method is executing.


8)If two signatures differ only because one declares a sequence and the other an array, then a compile-time error occurs.
Fixed-argument method will always be selected over a varargs method.


public class OverloadingWithVarargs {
	static void print(String str){
		System.out.println("Print 1: " + str);
	}
	static void print(String str, String...strings){
		System.out.println("Print 2: " + str);
		for(int i = 0;i < strings.length; ++i) System.out.println(strings[i]);
	}
	static void print(String...strings){
		System.out.println("Print 3: ");
		for(int i = 0;i < strings.length; ++i) System.out.println(strings[i]);
	}
	public static void main(String[] args){
		String str = "Java Beta"; // fixed parameter is prior to ones with varargs
//		print(str, "Java Beta"); // ambiguity: print 2 or print 3?
	}
}


9)static import statement: 
import static java.util.Math.exp
Then you can use exp instead of Math.exp in your program. A static import statement must appear at the start of a source file before any class or interface declaration.


static import on demand statement: 
import static static java.util.Math.*








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章