简体   繁体   English

声明类类型的变量而不初始化它

[英]Declaring an variable of class type without initializing it

I read somewhere, while reading about the System.out.print that in the System class, there is a declaration of 'out' as a PrintStream class type static variable as follows: public static final PrintStream out; 我在某处读到有关System.out.print ,在System类中有一个名为out的声明,作为PrintStream类类型的静态变量,如下所示: public static final PrintStream out;

This invoked a question in me that what exactly happens if we just declare a variable of a certain class type and not initialize it by not calling any constructor? 这引起了我的疑问,如果我们只声明某个类类型的变量,而不通过不调用任何构造函数来初始化它,那会发生什么? In above example 'out' is declared static and final, but I am looking for a generalized answer. 在上面的示例中,“ out”被声明为静态且最终的,但我正在寻找一个广义的答案。

This invoked a question in me that what exactly happens if we just declare a variable of a certain class type and not initialize it by not calling any constructor? 这引起了我的疑问,如果我们只声明某个类类型的变量,而不通过不调用任何构造函数来初始化它,那会发生什么?

Then like any other field, it starts off with its default value - which for references types (classes, interfaces, enums) is the null reference. 然后,像其他任何字段一样,它以其默认值开始-对于引用类型(类,接口,枚举),该null默认值。 From section 4.12.5 of the JLS : JLS的4.12.5节开始

Every variable in a program must have a value before its value is used: 程序中的每个变量在使用值之前都必须具有一个值:

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2): 每个类变量,实例变量或数组组件在创建时都会用默认值初始化(第15.9节,第15.10.2节):
  • For type byte , the default value is zero, that is, the value of (byte)0 . 对于byte类型,默认值为零,即(byte)0的值。
  • For type short , the default value is zero, that is, the value of (short)0 . 对于short类型,默认值为零,即(short)0的值。
  • For type int , the default value is zero, that is, 0 . 对于int类型,默认值为零,即0
  • For type long , the default value is zero, that is, 0L . 对于long类型,默认值为零,即0L
  • For type float , the default value is positive zero, that is, 0.0f . 对于float类型,默认值为正零,即0.0f
  • For type double , the default value is positive zero, that is, 0.0d . 对于double类型,默认值为正零,即0.0d
  • For type char , the default value is the null character, that is, '\' . 对于char类型,默认值为空字符,即'\'
  • For type boolean , the default value is false . 对于boolean类型,默认值为false
  • For all reference types (§4.3), the default value is null . 对于所有引用类型(第4.3节),默认值为null

System.out is a bit special - it's final, but can be changed via System.setOut . System.out有点特殊-它是最终的,但可以通过System.setOut进行更改。 I would try to avoid generalizing any other behaviour based on that. 我将尝试避免基于此概括任何其他行为。

When you create an object and store it in a variable, you don't actually store it. 创建对象并将其存储在变量中时,实际上并没有存储它。 You only get a pointer to a specific memory address, where the object is currently located. 您只能获得指向对象当前所在的特定内存地址的指针。

If you don't initialize an object, you get a null pointer. 如果不初始化对象,则会得到一个空指针。 That object simply doesn't exist, there are are no fields or methods in it. 该对象根本不存在,其中没有字段或方法。

Static fields and methods are different, they aren't connected to an instance of an object, they're connected to the class (this is one of the many reasons using static is a bad practise). 静态字段和方法是不同的,它们没有连接到对象的实例,而是连接到了类(这是使用静态方法的不好原因之一)。 They can be accesed from anywhere at anytime. 可以随时随地访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM