当获取这个静态变量的初始化值并且何时使用StringBuffer。

huangapple 未分类评论48阅读模式
英文:

When gets this static variable initalised and when to use StringBuffer

问题

我刚刚看到这个例子

public class Runtime {
    //这个私有成员什么时候被初始化??
    private static Runtime currentRuntime = new Runtime();


    public static Runtime getRuntime() {
        return currentRuntime;
    }

现在当你调用 Runtime.getRuntime() 时,currentRuntime 已经被初始化了,即使从未创建过该类的实例。它是如何工作的?

我第二个问题是关于 StringBuffer?

String var = "Hello World";

现在,如果你执行 var = var + "!";

是不是使用 StringBuffer 更好?我的意思是 StringBuffer 至少会预留很多内存,我认为至少有 1000 字节,所以仅针对这个简单的例子,不使用 StringBuffer 仍然更好。当然,现在我们在内存中有两个字符串 "Hello World" 和 "Hello World!",但仍然比使用至少 1000 字节的 StringBuffer 好。对吗?

英文:

I just saw this example

public class Runtime {
    //When is this private member initalised ??
    private static Runtime currentRuntime = new Runtime();


    public static Runtime getRuntime() {
        return currentRuntime;
    }

Now when you call Runtime.getRuntime() currentRuntime is already initalised even an instance of this class was never created. How does it work ?

My second question is about StringBuffer ?

String var = "Hello World";

Now if you do var = var + "!";

Whould it be better to use a StringBuffer. I mean StringBuffer reserves a lot of memory I think 1000Byte at least, so just for that simple example it would still be better no to use StringBuffer. Of course
now we have 2 Strings in memory "Hello World" and "Hello World!" but still better than StringBuffer with 1000 Byte or more . Right ?

答案1

得分: 0

我会回答这个问题:“何时初始化这个静态变量”

我已经体验到Java类在需要时被加载。例如,如果你从未使用过它(例如,你从未引用过一个变量,或者调用过一个函数/构造函数),JVM不会初始化这个类。

因此,当你调用Runtime.getRuntime()时,类被初始化,静态字段也被初始化。

你还可以使用下面的代码进行测试,其中包含了静态块。

public class MainClass {
    public static void main(String[] args) {
        System.out.println("程序开始");
        Runtime.getRuntime();
        System.out.println("程序结束");
    }
}
public class Runtime {
    // 这个私有成员何时初始化?
    private static Runtime currentRuntime = new Runtime();

    static {
        System.out.println("当这个被调用时,类正在被初始化");
        System.out.println("currentRuntime 是否为空? -> " + (currentRuntime == null));
    }

    public static Runtime getRuntime() {
        System.out.println("执行了 getRuntime");
        return currentRuntime;
    }
}

这是我在控制台上得到的输出:

程序开始
当这个被调用时,类正在被初始化
currentRuntime 是否为空? -> false
getRuntime 被调用
程序结束

这意味着什么?类Runtime(以及它的字段)在main()开始之前没有被初始化,但在请求Runtime时被初始化。

另外,静态块在currentRuntime不为空时被调用,因此它已经被初始化(记住Java是自上而下的)。

所以,它在调用Runtime.getRuntime()时被初始化,但在它执行之前被初始化。

英文:

I will answer this: "when gets this static variable initialized"

I've experienced that Java classes are loaded when they're needed. For example, if you never use it (for example, you never refer to a variable, or call a function/constructor) the JVM does not initialize the class.

So, when you call Runtime.getRuntime(), then the class is initialized and so are the static fields.

You can also test it using static blocks with this code

public class MainClass {
	public static void main(String[] args) {
		System.out.println("program starts");
		Runtime.getRuntime();
		System.out.println("programs end");
	}
}
public class Runtime {
	//When is this private member initalised ??
	private static Runtime currentRuntime = new Runtime();

	static {
		System.out.println("when this is called the class is beeing initialized");
		System.out.println("is currentRuntime null? -> " + (currentRuntime == null));
	}

	public static Runtime getRuntime() {
		System.out.println("getRuntime is executed");
		return currentRuntime;
	}
}

This is what i get on the console

program starts
when this is called the class is beeing initialized
is currentRuntime null? -> false
getRuntime is called
programs end

What does this mean? That the class Runtime (and its fields) are not initialized before main() starts, but it is initialized when Runtime is requested.

Also, the static block is called when currentRuntime is not null, so it's already initialized (remember that Java is top-down).

So, it's initialized when Runtime.getRuntime() is called, but before it's executed

huangapple
  • 本文由 发表于 2020年5月29日 17:00:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/62082261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定