简体   繁体   English

为什么初始化实例变量时我会得到零?

[英]why am i getting zero while i have initialized my instance variables?

i have a simple java program below. 我下面有一个简单的Java程序。 i have declared instance integer variable "x1" and "x2", initialize them from parameters in my class constructor. 我已经声明了实例整数变量“ x1”和“ x2”,并从我的类构造函数中的参数对其进行了初始化。 i have also declared an integer variable "diff" that works out the difference between "x1" and "x2" but the variable returns zero!! 我还声明了一个整数变量“ diff”,该变量计算出“ x1”和“ x2”之间的差异,但该变量返回零! i don't know where the problem is and my constructor parameters are not zero!!!. 我不知道问题出在哪里,我的构造函数参数也不为零! i will appreciate any help 我将不胜感激

public class Smallest {
int x1;
int x2; 

public Smallest(int a,int b){
 this.x1=a;
 this.x2=b;
}

int diff = x2 - x1;   
public void draw(){
    System.out.println("X1= "+ x1 +"\n X2= "+ x2 +"\n diff= " +diff);
}

public static void main(String[] args) {
Smallest small=new Smallest(10,20);
small.draw();
}
}

here's the output 这是输出

 X1= 10;
 X2= 20;
 diff= 0;

This is because diff is not inside any method it set during compile time. 这是因为diff不在编译期间设置的任何方法之内。 You should move it to the constructor or any other method to get correct value. 您应该将其移至构造函数或任何其他方法以获得正确的值。
Example : 范例:

int diff;
public Smallest(int a,int b){
   this.x1=a;
   this.x2=b;
   this.diff=x2 - x1;   
}

int diff = x2 - x1; should be inside the constructor or inside the method draw() (depending on what you want to do with it). 应该位于constructor内部或方法draw()内部(取决于您要使用的方法)。

Remember: By default int variables are initialized to 0. Before you call the constructor: 记住:默认情况下,int变量被初始化为0。 调用构造函数之前

x1=0; 
x2=0; 
diff=x1-x2
    =0-0
    =0

When you call the constructor you give different values to x1 and x2 but you don't calculate the diff on the newly updated values. 当您调用构造函数时,您给x1x2赋予了不同的值,但是您没有计算新更新值的diff

After you call the constructor: 调用构造函数后:

x1=10;
x2=20;
diff=0; // you don't recalculate it, it's not in the constructor or in the draw method.

Member variables are initialized before your constructor is called. 调用构造函数之前,先初始化成员变量。 Thus diff is equal to 0 - 0 since x1 and x2 have not been set yet. 因此,由于尚未设置x1和x2,因此diff等于0-0。

暂无
暂无

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

相关问题 为什么在 for 循环中初始化变量时,我会收到“变量可能没有为变量 a、b 和 n 初始化”? - Why am I getting "variable might not have initialized for variables a,b and n" while they are initialized in a for-loop? 为什么我收到变量未初始化错误? - Why am I getting variable not initialized error? 为什么我在这个程序中收到“/ by zero”错误? - Why am I getting a “/ by zero” error in this program? 为什么我从静态块中初始化的构造函数中获取Hashmap时获取ExceptionInInitializerError - why I am getting ExceptionInInitializerError while acessing Hashmap from constructor which is initialized in static block Java 为什么我在实例化我的数组时得到 NullPointerException - Java Why am I getting a NullPointerException while instantiating my array 为什么我总是时差为零? - Why am I always getting a time difference of zero? 为什么我得到java.lang.ArithmeticException:/减零? - Why am I getting java.lang.ArithmeticException: / by zero? 为什么我得到 java.lang.ArithmeticException:除以零 - Why am I getting java.lang.ArithmeticException: divide by zero 为什么即使我在proguard-project.txt中添加了jar文件,也出现此错误? - Why i am getting this errors even i have added my jar files in proguard-project.txt? 当我在类的构造函数中声明并初始化它们时,为什么我的字段被初始化为 null 或默认值零? - Why are my fields initialized to null or to the default value of zero when I've declared and initialized them in my class' constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM