Java构造函数没有名字?

发布于 2024-11-02 10:18:51 字数 411 浏览 2 评论 0原文

当我运行下面的代码时,我得到的输出为:

static block
TEst block
main block

How does the string "TEst block" gets print?它被视为构造函数吗?

public class TestBlk {

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

{
    System.out.println("TEst block");
}


public static void main(String args[]){
    TestBlk blk=new TestBlk();
    System.out.println("main block");

}
}

When I run the below code I am getting output as:

static block
TEst block
main block

How does the string "TEst block" gets printed? Is it considered as constructor?

public class TestBlk {

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

{
    System.out.println("TEst block");
}


public static void main(String args[]){
    TestBlk blk=new TestBlk();
    System.out.println("main block");

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

握住我的手 2024-11-09 10:18:51

它是一个实例初始化程序,以及一个默认构造函数。

没有显式构造函数的类会被赋予一个合成的、公共的、无参数的构造函数。

没有调用 this()super()(可能带参数)的构造函数会隐式调用 super()(不带参数) ,内部类可能会发生一些奇怪的事情)。

在隐式或显式调用 super() 之后,字段初始化程序和实例初始化程序中的所有代码都会按照其在源代码中出现的顺序插入。

因此,在 javac 完成代码处理后,它看起来有点像:

public class TestBlk {

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

    public TestBlk() {
        // Call constructor of java.lang.Object.
        super();

        // From instance (and field)initialiser.
        System.out.println("TEst block");

        // Rest of constructor:
    }


    public static void main(String args[]){
        TestBlk blk = new TestBlk();
        System.out.println("main block");
    }
}

It's an instance initialiser, together with an default constructor.

A class without an explicit constructor, is given a synthetic, public, no-args constructor.

Constructors without a call to this() or super() (possibly with arguments) are given an implicit call to super() (without arguments, possibly something odd happens with inner classes).

Immediately after an implicit or explicit call to super(), all the code in field initialisers and instance initialisers gets inserted in the order it appears in the source code.

So after javac has finished with your code it looks a little like:

public class TestBlk {

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

    public TestBlk() {
        // Call constructor of java.lang.Object.
        super();

        // From instance (and field)initialiser.
        System.out.println("TEst block");

        // Rest of constructor:
    }


    public static void main(String args[]){
        TestBlk blk = new TestBlk();
        System.out.println("main block");
    }
}
玉环 2024-11-09 10:18:51

这里的内容称为:初始化块。

初始化块是一个块
执行的大括号之间的代码
在类的对象之前
创建。

有两种类型的初始化块:

  1. 非静态初始化块。

    <代码>{
    System.out.println("测试块");
    }

  2. 静态初始化块。

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

的解释:

请注意,任何初始化块
类中存在的被执行
在构造函数之前

现在的问题是我们为什么需要
构造函数(如果我们有初始化块)。答案是我们不需要默认值
构造函数但初始化块
无法参数化,所以你
不能让对象从中获取值
外侧等初始化块
不能替代构造函数。

What you have here is called: initialization block.

An initialization block is a block of
code between braces that is executed
before the object of the class is
created.

There are two types of initialization blocks:

  1. Non static initialization block.

    {
    System.out.println("TEst block");
    }

  2. Static initialization block.

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

More specific, I like the explanation from here:

Note that any initialization block
present in the class is executed
before the constructor
.

So now the question comes why we need
constructors if we have initialization blocks. The answer is we don't need the default
constructor but initialization block
cannot be parameterized
and so you
cannot have objects taking values from
out side and so initialization block
is not a substitute for constructor.

‖放下 2024-11-09 10:18:51

当您调用 new TestBlk() 时,它会作为对象构造的一部分被调用。

It is called as a part of object construction when you call new TestBlk().

两仪 2024-11-09 10:18:51
{
    System.out.println("TEst block");
}

这是一个初始化块。请参阅我的对另一个问题的回答

{
    System.out.println("TEst block");
}

That's an initialization block. See my answer to this other question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文