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.*








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值