static

               
学习的过程就是填坑的过程,可不要偷懒想着跳过去,你现在跳过去,就相当于给自己挖了一个坑,你迟早会掉进去的,为了避免不掉坑,所以,努力填坑吧!

 

一.如果没有static会怎样?

需求:  

1:定义Student类

 

      1:姓名、国籍,说话行为

 

      2:多个构造,重载形式体现

 

2:学生的国籍都是确定的

 

      1:国籍可以进行显示初始化
public class Student { String name;//姓名 String country;//国籍 public
Student(String name, String country) { this.name = name; this.country =
country; } public void speak(){ System.out.println("姓名:"+this.name+"
"+"国籍:"+this.country); } } class Test{ public static void main(String[] args) {
Student student = new Student("何道昌","中国"); Student student1 = new
Student("孙双双","中国"); student.speak(); student1.speak(); } }
  
运行结果: 姓名:何道昌 国籍:中国 姓名:孙双双 国籍:中国
  

目前存在的问题:

        现在我们已知学生都是中国人,现在我们每创建一个学生对象,就要给所有学生的国籍属性赋相同的值,这样造成堆内存空间资源浪费

目前方案:

        把“中国”这个数据移动到数据共享区中,共享这个数据给所有的Student对象使用即可

        疑问:如何才能把这个数据移动到数据共享区中共享呢?

解决方案:

        只需要用static修饰该数据即可

        静态的成员变量只会在数据共享区中维护一份,而非静态成员变量的数据会在每个对象中都维护一份

 

 
public class Student { String name;//姓名
//使用了static修饰country,那么这时候country就是一个共享的数据 static String country = "中国";//国籍
//构造函数 public Student(String name) { this.name = name; } //说话行为 public void
speak(){ System.out.println("姓名:"+this.name+" "+"国籍:"+country); } } class Test{
public static void main(String[] args) { Student student = new Student("何道昌");
Student student1 = new Student("孙双双"); student.speak(); student1.speak(); } }
  
运行结果: 姓名:何道昌 国籍:中国 姓名:孙双双 国籍:中国
  

下面我来详细解说static

 

static(静态修饰符)

1.static修饰静态变量

        如果有数据需要被共享给所有对象使用时,那么就可以使用static修饰

        静态成员变量的访问方式:

               方式一: 可以使用对象进行访问

                             格式:对象.变量名

               方式二:可以使用类名进行访问

                             格式:类名.变量名

               注意:

                         1.非静态的成员变量只能使用对象进行访问,不能使用类命进行访问

 
public class Student { String name;//姓名 非静态成员变量
//使用了static修饰country,那么这时候country就是一个共享的数据 static String country = "中国";//国籍
静态成员变量 //构造函数 public Student(String name) { this.name = name; } //说话行为 public
void speak(){ System.out.println("姓名:"+this.name+" "+"国籍:"+country); } } class
Test{ public static void main(String[] args) { Student student = new
Student("何道昌"); System.out.println(student.name);//用对象访问非静态变量
Systen.out.println(student.country);//用对象访问静态变量
System.out.println(Student.country);//用类命访问静态变量
} }


              
运行结果: 何道昌
中国 中国
           

                 2.千万不要为了方便访问数据而使用static修饰成员变量,只有成员变量的数据真正需要被共享的时候,才使用static修饰

            static修饰成员变量的应用场景:如果一个数据需要被所有对象共享使用的时候,用static修饰

2.static修饰成员函数(静态的成员方法)

            

 静态成员函数的访问方式:

               方式一: 可以使用对象进行访问

                             格式:对象.静态的函数名

               方式二:可以使用类名进行访问

                             格式:类名.静态的函数名

 

推荐使用类名直接访问静态的成员

         原因:

                   1.方便      

                   2.节省内存

 

静态函数要注意的事项:

        1.静态函数是可以调用类名或者对象进行调用的,而非静态函数只能使用对象进行调用

        2.静态的函数可以访问静态的成员,但是不能直接访问非静态的成员

        3.非静态的函数是可以直接访问静态与非静态的成员

        4.静态函数不能出现this或者super关键字

 
public class Student { String name;//姓名 非静态成员变量
//使用了static修饰country,那么这时候country就是一个共享的数据 static String country = "中国";//国籍
静态成员变量 //构造函数 public Student(String name) { this.name = name; } //说话行为 //静态成员方法
public static void speak(){ System.out.println("国籍:"+country); } //学习行为
//非静态成员方法 public void study(){ System.out.println(name+"好好学习"); } } class Test{
public static void main(String[] args) { Student student = new Student("何道昌");
System.out.println(student.name);//用对象访问非静态变量
System.out.println(student.country);//用对象访问静态变量
System.out.println(Student.country);//用类命访问静态变量 student.study();//用对象访问非静态方法
student.speak();//用对象访问静态方法 Student.speak();//用类名访问静态方法 } }
  
运行结果: 何道昌 中国 中国 何道昌好好学习 国籍:中国 国籍:中国
  

最后,继续用这个例子穿插一下静态代码块的知识

           静态代码块是在Student.class文件加载到内存的时候就马上执行的
public class Student { String name;//姓名 非静态成员变量
//使用了static修饰country,那么这时候country就是一个共享的数据 static String country = "中国";//国籍
静态成员变量 //静态代码块 static{ System.out.println("静态代码块执行了!!"); } //构造函数 public
Student(String name) { this.name = name; } //说话行为 //静态成员方法 public static void
speak(){ System.out.println("国籍:"+country); } //学习行为 //非静态成员方法 public void
study(){ System.out.println(name+"好好学习"); } } class Test{ public static void
main(String[] args) { Student.speak(); } }
  
运行结果: 静态代码块执行了!! 国籍:中国
  

理解到这,你再看看下面的分析图,应该还会有所收获

 



 

 

最后再跟大家分享一句话:

          一个人若想改变自己的命运,最重要的是要改变自己,改变心态,改变环境,这样命运也会随之改变

          读完此篇,如若对你有用,记得推荐,如若有误,欢迎指正!

          此篇为本人原创,转载请说明出处

 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信