Java基础知识的澄清? [英] Clarification in Java basics?

查看:135
本文介绍了Java基础知识的澄清?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class test {

test(){
System.out.println(Constructor);
}

{
System.out.println(Hai);
}

}

公共类样本{

public static void main(String [] a){
test t = new test();
}

}

在上面的代码中为什么是大括号内给出的陈述((即)Hai)在执行构造之前打印。

解决方案

让我们表达一个更清晰的例子:

  public class Test {

static {
System.out .println(static initializer);
}

{
System.out.println(instance initializer);
}

public Test(){
System.out.println(constructor);
}

}

并按如下方式测试:

  public class Main {

public static void main(String [] args){
测试test1 =新测试();
测试test2 = new Test();
}

}

输出:

 静态初始化器
实例初始化器
构造函数
实例初始化器
构造函数

静态初始化程序在运行时只执行一次,特别是在加载类时。实例初始值设定项在构造函数之前的每个实例化期间执行。



您可以拥有多个,它们将按照编码中的顺序执行。



实例初始化程序的主要好处是无论您使用哪种构造函数,它们都会被执行。它们适用于每一个,因此您不需要在所有这些上复制常见的初始化。



静态初始化器的主要好处是它们只执行一次在课堂加载期间。一个众所周知的现实世界的例子是JDBC驱动程序。当你这样做时

  Class.forName(com.example.jdbc.Driver); 

只执行 static 初始值设定项,然后任何(体面的)JDBC驱动程序将在 DriverManager 中注册自己,如下所示

  static {
DriverManager.registerDriver(new com.example.jdbc.Driver());这是 DriverManager 可以在 getConnection()期间找到正确的JDBC驱动程序。


class test {

    test() {
        System.out.println("Constructor");
    }

    {
        System.out.println("Hai");
    }

}

public class sample {

    public static void main(String [] a) {
        test t = new test();        
    }

}

in the above code why is that the statement given within the braces ((i.e) "Hai") is Printed before the constuctor is executed.

解决方案

Let express with a more clear example:

public class Test {

    static {
         System.out.println("static initializer");
    }

    {
         System.out.println("instance initializer");
    }

    public Test() {
         System.out.println("constructor");
    }

}

and test it as follows:

public class Main {

    public static void main(String[] args) {
        Test test1 = new Test();
        Test test2 = new Test();
    }

}

output:

static initializer
instance initializer
constructor
instance initializer
constructor

The static initializers are executed only once during runtime, specifically during loading of the class. The instance initializers are executed during every instantiation before the constructor.

You can have more than one of them and they will be executed in the order as they appear in the coding.

The major benefit of instance initializers is that they are executed regardless of which constructor you use. They applies on each of them so that you don't need to duplicate common initialization over all of them.

The major benefit of static initializers is that they are executed only once during class loading. A well known real world example is the JDBC driver. When you do

 Class.forName("com.example.jdbc.Driver");

which only executes the static initializers, then any (decent) JDBC driver will register itself in the DriverManager as follows

 static {
      DriverManager.registerDriver(new com.example.jdbc.Driver());
 }

this way the DriverManager can find the right JDBC driver during getConnection().

这篇关于Java基础知识的澄清?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆