Java 构造函数 - 为变量赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15625833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:17:41  来源:igfitidea点击:

Java Constructors- Assigning value to variable

javaconstructor

提问by user2009020

Is there a difference between assigning values to variables outside of any method, and assigning these values within a constructor?

在任何方法之外为变量赋值与在构造函数中分配这些值之间有区别吗?

Looking at Oracle's Java tutorial, they have:

查看 Oracle 的 Java 教程,他们有:

public class Bicycle {

   int cadence = 0;
   int speed = 0;
   int gear = 1;

   void changeCadence(int newValue) {
      cadence = newValue;
   }

is this any different from saying/why didn't they just say:

这与说/他们为什么不说有什么不同:

Bicycle(){
    int cadence = 0;
 } 

回答by Todd

If you declare the variable in your constructor, it will be local to the constructor and not visible anywhere else in your class.

如果在构造函数中声明变量,它将是构造函数的本地变量,并且在类中的其他任何地方都不可见。

回答by Amir Pashazadeh

Well if you define a variable in a constructor, it is local to the constructor. But a variable in the class is part of class' state.

好吧,如果你在构造函数中定义了一个变量,它是构造函数的本地变量。但是类中的变量是类状态的一部分。

But if you mean:

但如果你的意思是:

class A {
    int i = 1;
    ...
}

vs

对比

class B {
    int i;

    B() {
        i = 1;
    }
    ...
}

the difference is: in the first case, the default value of iis 1, in all instances, but in second case the default value of iis 1 if the shown constructor is called, maybe in other constructors other default value be something else (or 0 if nothing is assigned to i).

区别在于:在第一种情况下,i在所有情况下,的默认值都是 1,但在第二种情况下,i如果调用显示的构造函数,则默认值是 1,也许在其他构造函数中,其他默认值是别的东西(或 0如果没有分配给i)。

回答by BloodShura

If you declare a variableinside a constructor, this variable can only be accessed inside this constructor. But, you can create a variable on your class, and access it on your constructoror method.

如果variable在 a 中声明 a constructor,则只能访问此变量inside this constructor。但是,您可以在您的 上创建一个变量class,并在您的constructor或上访问它method

回答by evgenyl

When you create object instance, global variables will be initialized. You can (but not have to) change some of them in constructor.

创建对象实例时,将初始化全局变量。您可以(但不是必须)在构造函数中更改其中的一些。

I think you mean

我想你的意思

Bicycle() { 
   cadence = 0; 
} 

回答by Alya'a Gamal

In the constructor : it will be Local variable

在构造函数中:它将是局部变量

Java contains the following types of variables:

Java 包含以下类型的变量:

Instance Variables (Non-static fields):In object oriented programming, objects store their individual states in the "non-static fields" that is declared without the static keyword. Each object of the class has its own set of values for these non-static variables so we can say that these are related to objects (instances of the class).Hence these variables are also known as instance variables. These variables take default values if not initialized.

实例变量(非静态字段):在面向对象的编程中,对象将它们各自的状态存储在没有 static 关键字声明的“非静态字段”中。类的每个对象对于这些非静态变量都有自己的一组值,因此我们可以说它们与对象(类的实例)相关。因此这些变量也称为实例变量。如果未初始化,这些变量采用默认值。

Class Variables (Static fields):These are collectively related to a class and none of the object can claim them its sole-proprietor . The variables defined with static keyword are shared by all objects. Here Objects do not store an individual value but they are forced to share it among themselves. These variables are declared as "static fields" using the static keyword. Always the same set of values is shared among different objects of the same class. So these variables are like global variables which are same for all objects of the class. These variables take default values if not initialized.

类变量(静态字段):这些变量与一个类共同相关,任何对象都不能声称它们是它的独资者。使用 static 关键字定义的变量由所有对象共享。在这里,对象不存储单个值,但它们被迫在它们之间共享它。这些变量使用 static 关键字声明为“静态字段”。始终在同一类的不同对象之间共享相同的一组值。所以这些变量就像全局变量,对于类的所有对象都是相同的。如果未初始化,这些变量采用默认值。

Local Variables:The variables defined in a method or block of code is called local variables. These variables can be accessed within a method or block of code only. These variables don't take default values if not initialized. These values are required to be initialized before using them.

局部变量:在方法或代码块中定义的变量称为局部变量。这些变量只能在方法或代码块中访问。如果未初始化,这些变量不会采用默认值。这些值需要在使用前进行初始化。

Parameters:Parameters or arguments are variables used in method declarations.

参数:参数或参数是方法声明中使用的变量。

回答by Maroun

The constructor is like any other method. Variables that are declared inside it are available only within it, and they'll get destroyed when you go out of scope.

构造函数就像任何其他方法一样。在其中声明的变量仅在其中可用,当您超出范围时,它们将被销毁。

回答by Lucia Pasarin

The difference in this case is that you are not only assigning the variable in the constructor (in the second case). You are also declaring it there. Thus, the variable is local to the constructor and not visible from outside.

在这种情况下的不同之处在于您不仅在构造函数中分配变量(在第二种情况下)。你也在那里声明它。因此,该变量是构造函数的局部变量,从外部不可见。