简体   繁体   English

是否在函数外声明了所有实例变量?

[英]Are all instance variables declared outside functions?

I know that variables can be categorized in two ways:- 我知道变量可以用两种方式分类: -

The first way is to classify them into global and local variables based on their scope. 第一种方法是根据范围将它们分类为全局变量和局部变量 When the variable is accessible by all the methods of an instance of a class, ie, throughout the class, then it is known as a global variable and when it is accessible only within a block of code in an instance of a class it is known as local variable. 当变量可由类的实例的所有方法访问时,即在整个类中,则它被称为全局变量,并且当它仅在类的实例中的代码块内可访问时,它是已知的作为局部变量。

The the second way is to classify them into class/static instance/non-static variables . 第二种方法是将它们分类为类/静态实例/非静态变量 Class/static variables are those variables which belong to the class and only one copy of these variables exist for all instances of the class and it is shared by them. 类/静态变量是属于该类的变量,并且对于类的所有实例仅存在这些变量的一个副本,并且它们由它们共享。 Instance variables are those variables which belong to the instance of the class and for which a separate copy is created for each instance. 实例变量是属于类实例的变量,为每个实例创建单独的副本。

My instructor says that instance variables can only be declared outside functions. 我的讲师说实例变量只能在函数外声明。 Why is this so? 为什么会这样? Can local variables not be instance variables? 局部变量可以不是实例变量吗?

If you declare a variable inside a method, it's a local variable belonging to that method. 如果在方法中声明变量,则它是属于该方法的局部变量。 It will go out of scope when the method is terminated. 方法终止时,它将超出范围。 The only way to have a variable belong to an instance is to declare it directly under the class - ie, outside of any method. 使变量属于实例的唯一方法是直接在类下声明它 - 即,在任何方法之外。

EDIT: 编辑:

Here's a sample, as suggested by @Yeikel: 这是@Yeikel建议的样本:

public class MyClass {

    private static int iAmAStaticMember = 1;

    private int iAmAnInstanceMember;

    public void someMethod() {
        int iAmALocalVariables = 4;
    }
}

If they are declared inside a method, they are only in the method scope. 如果它们在方法内声明,则它们仅在方法范围内。 After the method run, the variable is destroyed. 方法运行后,变量将被销毁。

public class Something {

    int j = 0; // j lives as long as the class exists

    public doSomething() {
        int i = 0;
        // i is gone after method run
    }
}

Only global variables can be categorized into instance and static variables. 只有全局变量可以分为实例和静态变量。 Variables inside functions are local to the functions and belongs neither to class nor to object. 函数内部的变量是函数的局部变量,既不属于类也不属于对象。 Instance variables belong to object and static variables belong to class. 实例变量属于对象,静态变量属于类。

In Java, you have instance, static and local variables. 在Java中,您有实例,静态和局部变量。

Static variables are class level variables which belong to the class itself and thus one copy is maintained and used by all classes/objects. 静态变量是属于类本身的类级变量,因此所有类/对象都维护和使用一个副本。 They are brought alive when the class is loaded by the class loader and dies when the class has been unloaded. 当类加载器加载类时,它们会被激活,而当类被卸载时它们会死掉。

Instance variables are tied to an instance of a class, ie the object. 实例变量绑定到类的实例,即对象。 Thus there is a copy of the variable for each object created. 因此,每个创建的对象都有一个变量的副本。 Based on the access modifier, restriction is imposed on its usage outside the class (usually made private and accessed via getters and setters). 基于访问修饰符,对其在类外的使用施加限制(通常使其成为私有的并通过getter和setter访问)。 They are made alive when an instance is created and die when the garbage collector sees that the object does not have a valid/in-use reference pointing to it. 它们在创建实例时变为活动状态,并在垃圾收集器发现该对象没有指向它的有效/使用中引用时死亡。

Local variables are method level variables, ie they are local to the method. 局部变量是方法级变量,即它们是方法的本地变量。 These variables are created when the method is invoked (either in a static way or via an object reference) and die when the method execution is complete, or in other words, when the method has returned control to the caller. 这些变量是在调用方法时(以静态方式或通过对象引用)创建的,并在方法执行完成时死亡,或者换句话说,当方法将控制权返回给调用者时。

    class Demo {

    // static variable - can be accessed by any class/object
    public static int num1 = 1;

    // instance variable - accessed by all objects of this class; if made private, can use accessor methods to access it
    public int num2 = 2;

    // num3 is a local variable (method arguments are also local variables)
    public void getSum() {
        int num3 = 3;
        return num2 + num3;
    }

}

Hope this helps :) 希望这可以帮助 :)

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

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