一.static关键字
- static关键字:可以修饰成员变量和成员方法,静态的,共享的
/*
* 一个学生类:
* 成员变量:name,age,country(来自同一个国家)
* 成员方法:show()
* **/
class Student{
//成员变量
String name;
int age;
//String country;
//将country设为静态
static String country;
//两个参数的构造方法
public Student(String name,int age){
this.name = name;
this.age = age;
}
//三个参数的构造方法
public Student(String name,int age,String country){
this.name = name;
this.age = age;
this.country = country;
}
//成员方法
public void show(){
System.out.println(name+","+age+","+country);
}
}
public class Demo {
public static void main(String[] args){
//每次创建对象的时候都要给country赋值,在堆内存消耗内存较大,因此将country设为static
// Student s1 = new Student("王一",21,"中国");
// Student s2 = new Student("王二",22,"中国");
// Student s3 = new Student("王三",23,"中国");
//static静态的,共享的
Student s1 = new Student("王一",21,"中国");
Student s2 = new Student("王二",22);
Student s3 = new Student("王三",23);
s1.show();
s2.show();
s3.show();
System.out.println("---------------");
//country共享的,为最后一次赋的值
Student s4 = new Student("王一",21,"中国");
Student s5 = new Student("王二",22,"英国");
Student s6 = new Student("王三",23,"美国");
s4.show();
s5.show();
s6.show();
}
}
2. static关键字的特点:
static随着类的加载而加载
优先于对象存在
使用对象名可以去访问成员变量,如果这个成员被static修饰,那么可以被类名直接调用
class Number{
int num1 = 10;
static int num2 = 20;
public static void show(){
System.out.println("show");
}
}
public class Demo1 {
public static void main(String[] args){
//创建一个number类的对象
Number n = new Number();
//用对象名调用
System.out.println(n.num1+","+n.num2);
n.show();
//用static修饰的成员可用类名调用
System.out.println(Number.num2);
Number.show();
}
}
3. static的注意事项
在静态方法中是没有this关键字(静态随着类的加载而加载,this代表当前类的对象,随着对象的创建而存在)
静态的成员:静态只能访问静态
静态成员变量:在静态成员方法中,只能访问静态的成员变量
静态成员方法:只能访问静态(成员方法/成员变量)
非静态成员方法:里面可以访问静态的成员
class Number1 {
//非静态成员变量
int num1 = 10;
//非静态成员变量
static int num2 = 20;
//非静态成员方法
public void method1(){
System.out.println("1");
}
//非静态的成员方法
public static void method2(){
System.out.println("2");
}
//非静态成员方法
public void show1(){
System.out.println(num1);//隐藏了是访问该类对象的成员变量
System.out.println(this.num1);//明确了是访问该类对象的成员变量
System.out.println(num2);//访问静态成员变量
//调用静态/非静态的方法
method1();
method2();
System.out.println("------------");
}
//静态的成员方法
public static void show2(){
// System.out.println(num1);//报错,静态只能访问静态
System.out.println(num2);
// method1();//报错,静态只能访问静态
method2();
System.out.println("------------");
}
}
public class Demo2{
public static void main(String[] args){
Number1 n = new Number1();
n.show1();
n.show2();
System.out.println(n.num1);
System.out.println(n.num2);
}
}
二.制作帮助文档
制作文档说明书的步骤(用制作数组文档举例):
1. 定义一个数组工具类:ArrayTool(数组工具类中的注释:使用文档注释:解释每一个方法的参数是什么意思)
2. 定义一个测试类ArrayDemo
3. 编辑完这两个代码后新建文件夹,在其中建立两个文本文档,删去包名
4. 打开dos控制台—->切换到当前盘符—–>javadoc -d 目录名 -author -version ArrayTool.java
ArrayTool类:
/**
* 数组的一个工具类
* @author app
* @version V1.0
* */
public class ArrayTool {
/**
* 工具类的无参构造方法
* */
//为了不让外界创建对象,将无参构造私有化,将其他方法用static修饰,直接使用类名调用
private ArrayTool(){
}
/**
* 数组的遍历方法
* @param arr:需要被遍历的数组
* */
public static void printArray(int[] arr){
System.out.print("[");
for(int i = 0;i < arr.length;i ++){
if(i == arr.length-1){
System.out.println(arr[i]+"]");
}else{
System.out.print(arr[i]+",");
}
}
}
/**
* 找出数组最大值的方法
* @param arr:需要输出最大值的数组
* @return max:数组的最大值
* */
public static int printMax(int[] arr){
int max = arr[0];
for(int i = 0;i < arr.length; i++){
if(max < arr[i]){
max = arr[i];
}
}
return max;
}
}
ArrayDemo类:
public class ArrayDemo {
public static void main(String[] args){
int[] a = {10,10,30,40,50};
ArrayTool.printArray(a);
System.out.println(ArrayTool.printMax(a));
}
}
重新建立文件夹下的文本文档存储这两个类(注意删去这两个类的包名)
建立文档说明书文件夹,并在其下建立两个文本文档
输入dos命令:
在上述建立的文档说明书文件夹中生成目录文件夹
将目录文件夹下的ArrayTool.html用浏览器打开,即可看到制作好的文档说明书
三.代码块
- **代码块:**java中,用{}括起来的叫代码块
- 代码块的分类:
- 局部代码块:用来限定变量的生命周期,在main方法中出现
- 构造代码块:类中的成员位置,用来给对象的数据进行初始化.每次执行构造方法之前,要执行构造代码块
- 静态代码块:类中的成员位置,用static修饰代码块.
静态代码块的作用:静态随着类的加载而加载,给类进行初始化
- 静态代码块,构造代码块,构造方法,执行的顺序?
静态代码块—>构造代码块—>构造方法
分析:静态代码块:在类中只加载一次,随着类的加载而加载.构造代码块:每次在执行构造方法之前,先走构造代码块,随着对象的加载而加载.因此顺序是:静态代码块—>构造代码块—>构造方法
package 代码块;
class Code{
//静态代码块
static{
int a = 1000;
System.out.println(a);
}
//构造代码块
{
int b = 100;
System.out.println(b);
}
//无参构造
public Code(){
System.out.println("code");
}
//有参构造
public Code(int c){
System.out.println("code..");
}
//构造代码块
{
int d = 10;
System.out.println(d);
}
//静态代码块
static{
int e = 0;
System.out.println(e);
}
}
public class Demo1 {
public static void main(String[] args){
//局部代码块
{
int f = 1;
System.out.println(f);
}
// System.out.println(f);//报错:局部变量限定变量的声明周期
{
int g = 0;
System.out.println(g);
}
System.out.println("-----------");
Code c1 = new Code();
System.out.println("-----------");
Code c2 = new Code();
System.out.println("-----------");
Code c3 = new Code();
}
}
分析:
在main方法中按照顺序先执行两个局部代码块,输出1,0.
Code c1 = new Code();静态代码块随着类的加载而加载,先执行Code类中的静态代码块,按照顺序输出1000,0.再执行构造方法之前,先执行构造代码块,输出100,10.最后执行无参构造方法,输出code.
Code c2 = new Code();静态代码块只在类中加载一次.因此,执行构造方法之前,先执行构造代码块,输出100,10.最后执行无参构造方法,输出code.
Code c3 = new Code();静态代码块只在类中加载一次.因此,执行构造方法之前,先执行构造代码块,输出100,10.最后执行无参构造方法,输出code.