1 作用域
java中作用域的范围由花括号标示。
2关于static
垃圾收集器只知道搜集那些有new运算符分配的内存,不知道如何释放特殊内存。
如果垃圾收集器准备回收对象的内存,它首先调用finalize(),而且只有在下一次垃圾搜集过程中,才会真正回收对象的内存。所以说如果使用
finalize()就会完成重要的清除和清扫工作。
要点一 垃圾收集不等于破坏。
要点二 我们的对象可能不会当做垃圾被收掉。
3 finalize()的用途
垃圾收集只跟内存有关。
并不是清除工作的理想场所
最有用的一点就是观察垃圾搜集的过程。
4规定初始化
(1) 可以在类内部定义变量的同时,为其赋值 ,但这在c++中试不可以的。当然,也可以初始化非基本变量。甚至可以调用一个方法。
(2) 初始化顺序
在一个类中,初始化的顺序是变量在类内定义的顺序决定的。即使变量定义大量遍布于方法定义的中间,那些变量仍然会在调用任何方法之前被调用,甚至在构造函数之前。
实例代码
package cn.bupt.duming;
class Tag{
Tag(int marker){
System.out.println("Tag("+marker+")");
}
}
class Card{
Tag t1 = new Tag(1);
Card(){
System.out.println("Card()");
t3 = new Tag(33) ;
}
Tag t2 = new Tag(2);
Tag t3 = new Tag(3);
void f(){
System.out.println("f()");
}
}
public class OrderOfInitialization {
/**
* @param args
*/
public static void main(String [] args){
Card t = new Card();
t.f();
}
}
结果为:
Tag(1)
Tag(2)
Tag(3)
Card()
Tag(33)
f()
2静态数据的初始化
由于static 值,只有一个存储区域,所以无论创建多少对象,都会遇到对那个存储区域进行初始化的问题。下面为代码实例
package cn.bupt.duming;
class Bowl{
Bowl(int marker){
System.out.println("Bowl("+marker+")");
}
void f(int marker){
System.out.println("f("+marker+")");
}
}
class Table{
static Bowl b1 = new Bowl(1) ;
Table(){
System.out.println("Table()") ;
b2.f(1) ;
}
void f2(int marker){
System.out.println("f2("+marker+")");
}
static Bowl b2 = new Bowl(2);
}
class Cupboard{
Bowl b3 = new Bowl(3);
static Bowl b4 = new Bowl(4) ;
Cupboard(){
System.out.println("Cupboard()");
b4.f(2) ;
}
void f3(int marker){
System.out.println("f3("+marker+")");
}
static Bowl b5 = new Bowl(5);
}
public class StaticInitialization {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Creating new Cupboard() in main");
new Cupboard();
t2.f2(1) ;
t3.f3(1) ;
}
static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
}
结果为 :
Bowl(1)
Bowl(2)
Table()
f(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f(2)
f2(1)
f3(1)
3 明确进行的静态初始化
static 可以包含一个从句,尽管这看起来像一个方法。static包含的语句,只执行一次
class Cup{
Cup(int marker){
System.out.println("Cup("+marker+")");
}
void f(int marker){
System.out.println("f("+marker+")");
}
}
class Cups{
static Cup c1 ;
static Cup c2 ;
static {
c1 = new Cup(1) ;
c2 = new Cup(2) ;
}
Cups(){
System.out.println("Cups()");
}
}
public class Exp{
public static void main(String [] args){
System.out.println("Inside main()");
Cups.c1.f(99) ;
}
static Cups x = new Cups() ;
static Cups y = new Cups() ;
/*
结果为
static之间的语句,只执行一次
Cup(1)
Cup(2)
Cups()
Cups()
f(99)
*/
}
4非静态实例的初始化
只需把上例中static去除掉
5 数组初始化
java可以将一个数组分配给另一个,
int [] a1 = {1 , 2, 3 ,4,5} ;
int [] a2 ;
a2 = a1 ;
public class Array{
public static void main(String [] args){
int [] a1 = {1,2,3,4,5} ;
int [] a2 ;
a2 = a1 ;
for(int i = 0 ; i < a2.length ; i++ ){
a2[i]++ ;
System.out.println(a2[i]);
}
System.out.println();
for(int i = 0; i < a1.length ;i++){
System.out.print(a1[i]);
}
System.out.println();
}
}
两次打印的结果相同,都为 2 3 4 5 6