简体   繁体   中英

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:

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.

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). 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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