zoukankan      html  css  js  c++  java
  • Java静态相关问题

    问题1:

    public class Demo01_StaticTest {
        
        private static Demo01_StaticTest st = new Demo01_StaticTest();
    
        public static int count1;
    
        public static int count2 = 0;
    
        private Demo01_StaticTest(){
            count1++;
            count2++;
        }
    
        public static Demo01_StaticTest getInstance(){
            return st;
        }
    
        public static void main(String[] args) {
            Demo01_StaticTest st = Demo01_StaticTest.getInstance();
    
            System.out.println("count1: " + st.count1);
            System.out.println("count2: " + st.count2);
        }
        
    }
    

      

        2.分析

      • main方法中的顺序开始分析
        Demo01_StaticTest st = Demo01_StaticTest.getInstance();
        
        这是一个静态方法,返回值是当前类中新定义的实例对象,
        private static Demo01_StaticTest st = new Demo01_StaticTest();
        
        执行顺序先执行无参构造方法
        
        private Demo——StaticTest()
        {
        count1++;
        count2++;
        }
        
        构造函数执行 count1/2初始值都是0 ,自增后都+1   此时count1 = 1,count2= 1;
        
        构造函数外,除count1 无引用之外,count2重新引用指向0.
        也就是方法参数的几个条件:
        1 一个方法不能改变一个基本数据类型的参数
        2 一个方法可以改变一个对象的参数状态
        3 一个方法不能让对象参数引用一个新对象

         Demo01_StaticTest st = Demo01_StaticTest.getInstance();
        执行到这 返回的count1 count2 为1 , 0;
        
        

        • 3.问题变化

          public class Demo02_StaticTest {
          
              public static int count1;
          
              public static int count2 = 0;
          
              private static Demo02_StaticTest st = new Demo02_StaticTest();
          
              private Demo02_StaticTest(){
                  count1++;
                  count2++;
              }
          
              public static Demo02_StaticTest getInstance(){
                  return st;
              }
          
              public static void main(String[] args) {
                  Demo02_StaticTest st = Demo02_StaticTest.getInstance();
          
                  System.out.println("count1: " + st.count1);
                  System.out.println("count2: " + st.count2);
              }
          }
          

          4.分析

          • 完全一样的分析方法
          • 最后的值为1,1

            二、例2:父类子类中均有静态代码块

            public class Demo03_StaticTest {
                public static void main(String[] args) {
                    new Child();//请问会输出些什么,以及顺序
                }
            }
            
            class Parent{
                static String name = "hello";
                static {
            
                    System.out.println("parent static block");
                }
            
                public Parent(){
                    System.out.println("parent constructor");
                }
            
            }
            
            class Child extends Parent{
            
                static  String childNam = "word";
                static {
            
                    System.out.println("child static block");
                }
            
                public Child(){
                    System.out.println("child constructor");
                }
            }
            

            2.分析

            • 在创建子类`new Child()的时候,会按下述的顺序执行
            • 首先去父类中看是否用静态相关的东西,有就先执行
            • 然后再看子类中是否用静态相关的东西,有就执行(以上两步可以总结为静态先行)
            • 然后再去执行父类的构造函数
            • 最后再执行子类的构造函数
            • 所以最后的输出是:

              3另一个重要的问题

              • 当子类定义构造方法的时候,它首先会去找父类中不带参数的构造方法

              • 所以父类中不带参数的构造方法永远先执行

            如果父类没有不带参数的构造方法,必须显示的调用,指定调用父类的哪一个构造方法
            • 这里有个问题,为什么必须要先执行父类的构造方法呢?

                  可以简单的理解,没有父类哪来的子类呢!
    问题5:

    public class A {
        static {
            System.out.println("a");
        }
    
        public static String H = "hello";
    
        public A() {
        }
    }
    
    public class B extends A {
        
        static {
            System.out.println("B");
        }
    }
    

      

    public class MainDemo {
        public static void main(String[] args) throws ClassNotFoundException {
            String s = B.H;
    
        }
    }
    

      分析:

        这里很容容易思维定式:认为B.H;是新建了B对象   从而就父类静态代码块 子类静态代码块 父类构造方法 子类构造方法 
        

     这里并没有涉及到B类的实例化,因为静态变量在类之间是共享的,前提是需要在静态方法中调用  非静态可以使用静态,静态不能使用非静态

    新鲜刺激的东西永远都有,玩之前掂量掂量自己几斤几两
  • 相关阅读:
    多表链接 Left join
    v2013调试无法访问此网站 localhost 拒绝了我们的连接请求
    随机不重复流水号。
    通过location对象的某些属性得到一个完整URL的各个部分。
    Vue.JS学习笔记
    前端名词录
    React学习札记一
    kraken-ejs创建一个项目【学习札记】
    express 学习札记
    nth-child & nth-of-type区别
  • 原文地址:https://www.cnblogs.com/banxianer/p/14428986.html
Copyright © 2011-2022 走看看