java 如何在java编程中将最近的值存储在变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15675315/
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:28:04  来源:igfitidea点击:

how to store recent values in a variable in java programming?

java

提问by Rajendra_Prasad

In my java program,I need to store the recent values in a variable and my code is as shown below

在我的java程序中,我需要将最近的值存储在一个变量中,我的代码如下所示

public class Exmp2 
{

        int noOfInstances;
    public Exmp2() 
    {
        noOfInstances++;
    }
    public static void main(String[] args){
        Exmp2 e1=new Exmp2();
        System.out.println("No. of instances for sv1 : " + e1.noOfInstances);

        Exmp2 e2=new Exmp2();
        System.out.println("No. of instances for sv1 : "  + e2.noOfInstances);
        System.out.println("No. of instances for st2 : "  + e2.noOfInstances);

        Exmp2 e3=new Exmp2();
        System.out.println("No. of instances for sv1 : "  + e3.noOfInstances);
        System.out.println("No. of instances for sv2 : "  + e3.noOfInstances);
        System.out.println("No. of instances for sv3 : "  + e3.noOfInstances);
    }
}

My output should be 1 2 2 3 3 3 but am getting 1 1 1 1 1 1 can you give solution?

我的输出应该是 1 2 2 3 3 3 但我得到 1 1 1 1 1 1 你能给出解决方案吗?

回答by SudoRahul

Declare your noOfInstancesvariable as static.

将您的noOfInstances变量声明为static.

static int noOfInstances;

Since its not static, for every new Exmp2()a noOfInstancesis created for that instance, with the default value as 0.

由于它不是static,因此为该实例创建了每个new Exmp2()a noOfInstances,默认值为0

回答by gefei

you havew to declare noOfInstancesto be static

你必须声明noOfInstances是静态的

    static int noOfInstances;

otherwise every new instance create by newwill have its own value of noOfInstances, beginning at 0 again

否则创建的每个新实例new都将有自己的值noOfInstances,再次从 0 开始

回答by Joey Roosing

noOfInstances should be declared static. For example:

noOfInstances 应声明为静态。例如:

static int noOfInstances;

This might be an interesting read. It should have an example with a similar situation as yours:

这可能是一个有趣的阅读。它应该有一个与您的情况类似的示例:

static keyword

静态关键字

In short, static literally makes a variable shared among instances of a given class. If you have a variable that is not static every instance will have its own private value for that variable. While a statically declared variable will have the same value in all instances of a class.

简而言之,静态字面上使变量在给定类的实例之间共享。如果您有一个不是静态的变量,那么每个实例都会为该变量拥有自己的私有值。而静态声明的变量在类的所有实例中将具有相同的值。

回答by Narendra Pathai

The variable should be declared static.

变量应该被声明static

Why?Because presently your variable noOfInstancesis not staticand for each instance of your class you create noOfInstances variable will also be created and always the value will be 1. So on declaring it static it is shared between all the instances of this class and will have the correct value.

为什么?由于目前您的变量noOfInstances不是静态的,为您创造noOfInstances变量也将被创建,总是值为1所以上声明为静态它是这个类的所有实例之间共享,将有正确的类的每个实例价值。

Static variables are created when the class is loaded and are shared across all the instances.

静态变量在类加载时创建,并在所有实例之间共享。

回答by Narendra Pathai

You have to declare you variable as static: staticvariable always stores the recent values,its static variable would store in static pool

您必须将变量声明为静态static变量始终存储最近的值,其静态变量将存储在static pool

static int noOfInstances;

回答by AmitG

Make noOfInstancesstaticlike given below,

noOfInstances静态像下面给出的,

static int noOfInstances; // static are shared among all objects created.

and no need to call e1.noOfInstancesinstead you can call Exmp2.noOfInstances

无需调用,e1.noOfInstances您可以调用Exmp2.noOfInstances

Instance (non-static) variable are copied in Objects whereas static variables are not copied in object. static are at the class level. Every object can see it.

实例(非静态)变量在对象中复制,而静态变量不在对象中复制。静态是在类级别。每个对象都可以看到它。

回答by aizaz

You Should declare your variable int noOfInstances;as static, in your code the instances are taking default value 0.

您应该将变量声明int noOfInstances;static,在您的代码中,实例采用默认值 0。

回答by Joe2013

See the below example on how to use the static variable for instance count

请参阅以下示例,了解如何使用静态变量进行实例计数

package com.stackoverflow.test;

public class Exmp2 {

    static int noOfInstances;

    public Exmp2() {
        noOfInstances++;
    }

    public static void main(String[] args) {
        System.out.println("No. of instances at this point "
                + Exmp2.noOfInstances);
        Exmp2 e1 = new Exmp2();
        System.out.println("No. of instances at this point "
                + Exmp2.noOfInstances);
        Exmp2 e2 = new Exmp2();
        System.out.println("No. of instances at this point "
                + Exmp2.noOfInstances);
        Exmp2 e3 = new Exmp2();
        System.out.println("No. of instances at this point "
                + Exmp2.noOfInstances);
    }
}

回答by G.S

Whenever you do new Exmp2();then noOfInstancesassigned to 0 as you are instantiating a new Object, Make noOfInstancesas staticso that its scope shifts to class level.

每当你new Exmp2();然后noOfInstances分配给0作为你实例化一个新的对象,使noOfInstances作为static让其范围转移到一流水平。