静态方法块 static 以及对象属性&类属性的用法

时间:2023-01-31 14:43:21

使用静态块的好处:只要在类被加载时,static块就会被调用,整个过程就调用这么一次,不会在后面的对象处又不断的调用。如果不使用它,就会出现如下问题:new一个对象,我就要调用一次所需的这些内容,重复被调用,从而增加开销。

程序1:这个程序就是用静态方法块,测试输入一个人的出生日期,判断是否在给定出生日期范围内出生,如果是就返回true,否则返回false。

package com.liaojianya.chapter5;

import java.sql.Date;

/**
* This program will demonstrate the use of staticBlock.
* @author LIAO JIANYA
*
*/
public class StaticBlock
{
public static void main(String[] args)
{
Person1 p = new Person1(Date.valueOf("1998-11-12"));
Person1 p1 = new Person1(Date.valueOf("1988-11-12")); System.out.println("Is this p born between 1980 and 1990:" + p.isBornBoomer());
System.out.println("Is this p1 born between 1980 and 1990:" + p1.isBornBoomer());
} } class Person1
{
private Date birthDate;
private static Date startDate, endDate;
static
{
startDate = Date.valueOf("1980-01-11");
endDate = Date.valueOf("1990-01-11");
}
public Person1(Date birthDate)
{
this.birthDate = birthDate;
} boolean isBornBoomer()
{
return birthDate.compareTo(startDate) >= 0 && birthDate.compareTo(endDate) < 0;
}
}

结果显示:

Is this p born between 1980 and 1990:false
Is this p1 born between 1980 and 1990:true

 TIP:先前写这个程序的时候,遇到的问题就是在Person1 p = new Person1();l

这个时候,我尝试在括号里赋日期值,报各种错误,然后才发现需要用到一个java.lang.sql里的Date.valueOf("YYYY-MM-DD")格式,而不能直接赋一个数字年份。

查了一下API如下:

public static Date valueOf(String s)

  将 JDBC 日期转义形式的字符串转换成 Date 值。

参数:
  s - 表示 "yyyy-mm-dd" 形式的日期的 String 对象
返回:
  表示给定日期的 java.sql.Date 对象
抛出:
  IllegalArgumentException - 如果给定日期不是 JDBC 日期转义形式 (yyyy-mm-dd)。

程序2:

package com.liaojianya.chapter1;
/**
* This program demonstrates the use of attribute of class.
* @author LIAO JIANYA
* 2016年7月20日
*/
public class UsingAttribute
{
static String a = "string-a";
static String b; String c = "string-c";
String d; static
{
printStatic("before static");
b = "string-b";
printStatic("after static");
} public static void printStatic(String title)
{
System.out.println("--------" + title + "--------");
System.out.println("a = \"" + a + "\"");
System.out.println("b = \"" + b + "\"");
} public UsingAttribute()
{
print("before constructor");
d = "string-d";
print("after constructor");
} public void print(String title)
{
System.out.println("--------" + title + "--------");
System.out.println("a = \"" + a + "\"");
System.out.println("b = \"" + b + "\"");
System.out.println("c = \"" + c + "\"");
System.out.println("d = \"" + d + "\""); }
public static void main(String[] args)
{
System.out.println();
System.out.println("------------create UsingAttribute object------------");
System.out.println();
new UsingAttribute();
System.out.println("----------------again---------------");
new UsingAttribute();
} }

  运行结果:

--------before static--------
a = "string-a"
b = "null"
--------after static--------
a = "string-a"
b = "string-b" ------------create UsingAttribute object------------ --------before constructor--------
a = "string-a"
b = "string-b"
c = "string-c"
d = "null"
--------after constructor--------
a = "string-a"
b = "string-b"
c = "string-c"
d = "string-d"
----------------again---------------
--------before constructor--------
a = "string-a"
b = "string-b"
c = "string-c"
d = "null"
--------after constructor--------
a = "string-a"
b = "string-b"
c = "string-c"
d = "string-d"

  分析:

  初始化顺序:

  类属性(静态变量)定义时的初始化,如static String a = "string-a"

  static 块中的初始化代码,如static{}中的b = “string-b”

  对象属性(非静态变量)定义时的初始化, 如String c = “string-c”

  构造方法中的初始化代码,如d ="string-d"

  1)static语句块用于初始化static成员变量,是最先运行的语句块,只要在类被加载时,static块就会被调用,整个过程就调用这么一次,不会在后面的对象处又不断的调用。

  2)被static修饰的变量称为类变量(class‘s variables),被类的实例所共享,某一个类的实例改变了这个静态值,其他这个类的实例也会受到影响。而成员变量(member variables)则是没有被static修饰的变量,为实例所私有,即每个类的实例都有一份自己专属的成员变量,只有当前实例才可以改变它们的值。