static类
- 概述:
- 关于 static 关键字的使用,它可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不是单单是属于某个对象的。也就是说,既然属于类,就可以不靠创建对象来调用了。
定义和使用格式
- 类变量:使用 static关键字修饰的成员变量。
- 当 static 修饰成员变量时,该变量称为类变量。该类的每个对象都共享同一个类变量的值。任何对象都可以更改该类变量的值,但也可以在不创建该类的对象的情况下对类变量进行操作。
- 使用格式:static 数据类型 变量名
- 举例:
static int numberId
- 调用演示,代码如下:
- 举例:
标准代码:
public class Student {
//成员变量
String name;
int age;
static String schoolNmae = "惠浦教育";
//构造方法
public Student() {
}
public Student(String name, int age, String schoolNmae) {
this.name = name;
this.age = age;
}
}
测试代码:
public class StudentTest {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student();
Student stu3 = new Student();
stu1.name = "宋柯";
stu1.age = 20;
//stu1.schoolNmae = "惠浦教育";
stu2.name = "周鹏";
stu2.age = 18;
//stu2.schoolNmae = "惠浦教育";
stu3.name = "嘉伟";
stu3.age = 19;
stu3.schoolNmae = "惠浦教育";
//System.out.println(stu1.schoolNmae);
}
}
-
静态方法
- 当 static 修饰成员方法时,该方法称为类方法 。静态方法在声明中有static ,建议使用类名来调用,而不需要创建类的对象。调用方式非常简单。
- 类方法:使用 static关键字修饰的成员方法,习惯称为静态方法。
- 定义格式:
修饰符 static 返回值类型 方法名(参数列表) {执行语句}
- 举例:
public static void test(){}
- 举例:
- 静态方法调用的注意事项:
- 静态方法可以直接访问类变量和静态方法。
- 静态方法不能直接访问普通成员变量或成员方法。反之,成员方法可以直接访问类变量或静态方法。
- 静态方法中,不能使用this关键字。
- 小贴士:静态方法只能方法静态成员。
-
调用格式
- 被static修饰的成员可以并且建议通过类名直接访问。虽然也可以通过对象名访问静态成员,原因即多个对象均属于一个类,共享使用同一个静态成员,但是不建议,会出现警告信息。
- 格式:
- 访问类变量:类名.类变量名
- 调用静态方法:类名.静态方法名(参数
package com.zhou.day0822.staticdemo;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;
public class Student {
public static void main(String[] args) {
Student s = new Student();
System.out.println(schoolNmae);
//省略了Student.schoolName
// 在static中不能直接访问非static的成员变量和成员方法
//study();//报错 对象名.成员方法名
s.study();
}
//成员变量
String name;
int age;
static String schoolNmae = "惠浦教育";
//构造方法
public Student() {
}
public Student(String name, int age, String schoolNmae) {
this.name = name;
this.age = age;
}
//成员方法
public void study() {//说明非静态可以调用静态方法,反过来不成立。
sing();//省略了this
}
public void sing() {//getMax先有 sing后有
getMax(1,2);//静态方法调用:1.对象名.方法名(); 2.类名。方法名(推荐);
}
public static void getMax(int a,int b) {
//eat();
}
public void eat() {//成员方法(非静态方法)的调用 只能是:对象名.方法名();
}
//难点 何时用static 现在一般来说工具类--->功能方法--->static
//通用的功能定义为static
//static属于所有对象
}
package com.zhou.day0822.staticdemo;
public class StudentTest {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student();
Student stu3 = new Student();
//stu1.test();
Student.getMax(1,2);
//static可以修饰方法 之后该方法变为类级别的方法 调用:1.对象名.方法名(); 2.类名.方法名(推荐使用);
stu1.name = "宋柯";
stu1.age = 20;
stu1.schoolNmae = "惠浦教育";
stu2.name = "周鹏";
stu2.age = 18;
stu2.schoolNmae = "惠浦教育";
stu3.name = "嘉伟";
stu3.age = 19;
stu3.schoolNmae = "惠浦教育";
System.out.println(stu1.schoolNmae);
//静态变量的访问:
//1.对象名.静态成员变量名
//2.类名.成员变量名(); 推荐使用这种
}
}
静态原理图解
- static 修饰的内容:
- 是随着类的加载而加载的,且只加载一次。存储于一块固定的内存区域(静态区),所以,可以直接被类名调用。它优先于对象存在,所以,可以被所有对象共享。
- 是随着类的加载而加载的,且只加载一次。存储于一块固定的内存区域(静态区),所以,可以直接被类名调用。它优先于对象存在,所以,可以被所有对象共享。
代码块
- 静态代码块:
- 定义在成员位置,使用static修饰的代码块{ }。
- 位置:类中方法外。
- 执行:随着类的加载而执行且执行一次,优先于main方法和构造方法的执行。
- 格式:
public calss ClassName {
static {
//执行语句
}
}
- 作用:给类变量进行初始化赋值。用法演示,代码如下:
package com.zhou.day0824;
//静态代码块
public class Demo01 {
public static void main(String[] args) {
//静态代码块主要作用:初始化数据
//典型场景 数据库驱动的加载
//被static修饰--->随着类的加载而加载
}
static {//代码块中 什么代码都可以写
//静态代码块只会执行一次
System.out.println("static===================");
}
}
- 构造代码块:
- 位置:类中方法外。
- 格式:
public calss ClassName {
{
//执行语句
}
}
- 作用:给类变量进行初始化赋值。用法演示,代码如下:
package com.zhou.day0824;
//构造代码块
public class Demo02 {
public static void main(String[] args) {
new Demo02();//只要new对象构造代码块就会执行一次 new几次执行几次
new Demo02();
new Demo02();
}
//构造代码块随着构造方法的调用执行
{
System.out.println("构造代码块!");
}
}
- 小贴士:static 关键字,可以修饰变量、方法和代码块。在使用的过程中,其主要目的还是想在不创建对象的情况下,去调用方法。下面将介绍两个工具类,来体现static 方法的便利。