栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

static关键字与异常

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

static关键字与异常

1. static关键字 1.1 static修饰成员变量

static 数据类型 变量名; 

       被static关键字修饰过的变量叫做静态变量,静态变量在对象被创建以前已经存在了,与对象没有关系

class Test {
	static String string;
}

public class Demo01 {

	public static void main(String[] args) {
		Test.string = "测试";
		System.out.println(Test.string);// 测试

		Test test = new Test();
		test.string = "对象";
		System.out.println(test.string);// 对象
		System.out.println(Test.string);// 对象

		Test.string = "改变";
		System.out.println(test.string);// 改变
		System.out.println(Test.string);// 改变
	}
}

        在使用类对象.属性进行赋值时会出现“The static field Test.string should be accessed in a static way”的提示信息,说明不符合规范,正确的方式是使用类名.属性调用。

1.2 static修饰成员方法

 public static void 方法名(参数){

}

        调用时,使用类名.方法名进行调用。

class Test {
	static String string;

	public static void test() {
		System.out.println("静态测试方法");
	}
}

public class Demo01 {

	public static void main(String[] args) {
		Test.test();// 静态测试方法

		Test test = new Test();
		test.test();// 静态测试方法
	}
}

       使用类对象.静态方法()调用时出现 The static method test() from the type Test should be accessed in a static way 说明不符合规范。

 1.3 static修饰代码块

 static{

}

        只要类加载,静态代码块就加载;静态代码块先于构造代码块执行。

class Test {
	public Test() {
		System.out.println("无参构造方法");
	}

	static {
		System.out.println("静态代码块");
	}

	{
		System.out.println("构造代码块");
	}
}

public class Demo01 {

	public static void main(String[] args) {
		Test test = new Test();
		
	}
}
 2. 异常

        Java针对异常提供了两种处理机制:捕捉和抛出

 2.1 异常的捕捉

try{
    可能出现异常的代码;
}catch(异常对象){

}finally{
    不管上方是否抛出异常,都要执行的代码;
}

public class Demo01 {

	public static void main(String[] args) {
		get1(2, 0);

		int[] s = new int[2];
		get2(2, 1, s);

		get3(2, 0, s);
	}

	public static void get1(int a, int b) {
		int num = 0;
		try {
			num = a / b;
		} catch (ArithmeticException e) {
			System.out.println(e.getMessage());// 输出错误信息
		} finally {
			System.out.println(num);
		}
	}

	public static void get2(int a, int b, int[] s) {
		int num = 0;
		try {
			num = a / b;
			s[2] = 3;
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println(e.getMessage());// 输出错误信息
			System.out.println(e.toString());
		} finally {
			System.out.println(num);
		}
	}

	public static void get3(int a, int b, int[] s) {
		int num = 0;
		try {
			num = a / b;
			s[2] = 3;
			
		} catch (Exception e) {
			System.out.println(e.getMessage());// 输出错误信息
		} finally {
			System.out.println(num);
		}
	}
}
2.2 异常的抛出

throw 动作,从方法中抛出一个错误,自定义异常也可以抛出

throws 方法声明处书写,告知使用者,此方法抛出了什么类型的异常

import java.io.FileNotFoundException;

public class Demo8 {
	public static void main(String[] args) throws Exception{
		test(0);
		Thread.sleep(1000);
	}
	public static void test (int a) throws FileNotFoundException{
		if (a == 0) {
			//编译时异常
			throw new FileNotFoundException();
		}
		System.out.println("运行了");
	}

}
 2.3 自定义异常

        如果Java提供的异常对象无法满足需求,就可以自定义异常对象。

import java.util.Scanner;

public class Demo01 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		int score = scanner.nextInt();
		scanner.close();
		try {
			test(score);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public static void test(int score) throws Exception {
		if (score > 100 || score < 0) {
			throw new Exception("输入整数有误");
		}
		if (score >= 90 && score <= 100) {
			System.out.println("优秀");
		} else if (score >= 80) {
			System.out.println("中等");
		} else if (score >= 70) {
			System.out.println("良好");
		} else if (score >= 60) {
			System.out.println("及格");
		} else {
			System.out.println("叫家长");
		}
	}

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032302.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号