2

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, i.e., 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?

MrAP
  • 223
  • 4
  • 18
  • `declared outside functions` ... there are no functions in Java, only methods. – Tim Biegeleisen Jan 03 '17 at 15:12
  • No, they cant. Their scope is limited to usage inside a method after they were declared in one. They are not accessible from other methods or objects. – f1sh Jan 03 '17 at 15:12
  • If a method being invoked on an object is instance or static, then I imagine any variables declared within the scope of that method would match that. Kind of a moot point though, since the scope of such variables would be only for that method invocation. – David Jan 03 '17 at 15:13
  • @TimBiegeleisen It is not a duplicate. Please read the question properly. I asked why local variables are not instance variables. – MrAP Jan 03 '17 at 15:21
  • I can't believe that this hasn't been asked and answered somewhere already on SO, but I have reopened. – Tim Biegeleisen Jan 03 '17 at 15:24
  • Why is this downvoted? – MrAP Jan 03 '17 at 15:50
  • @TimBiegeleisen If you believe that this has already been asked somewhere, please provide the link! – MrAP Jan 04 '17 at 01:29

4 Answers4

5

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 - i.e., 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;
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 3
    You should probably add sample code to complete this answer – Yeikel Jan 03 '17 at 15:14
  • But isn't local variable a part of the object. – MrAP Jan 03 '17 at 15:15
  • @MrAP: Depends on the semantics of how you define "part of the object" I suppose. The method is an instance method, so intuitively the scope of that method is connected to the instance of the object. On the other hand, if you're asking whether the values themselves are on the stack or on the heap, that's another story entirely. Does it really make a difference though? Is there a deeper purpose to what's being asked here? – David Jan 03 '17 at 15:27
  • 1
    When we speak about an object, the object carries a state, which are the instance variables, whereas methods define the behavior of the object. Thus you don't refer to a method level variable as a part of the object even though you may access it using an object reference, as the method may finish execution and the local variable removed from the stack space whereas the object is still alive in the heap memory. Thus you can relate an instance variable as belonging to the object as it stays as long as the object does. – Prathap Jan 03 '17 at 15:37
  • @David, you said "The method is an instance method, so intuitively the scope of that method is connected to the instance of the object." which implies that the local variables are belong to the instance of the class and thus for every instance a separate copy is created. So shouldn't they too be instance variable? – MrAP Feb 13 '17 at 08:28
1

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
    }
}
ppasler
  • 3,579
  • 5
  • 31
  • 51
1

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.

Shakhar
  • 432
  • 3
  • 12
1

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, i.e. 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, i.e. 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 :)

Prathap
  • 178
  • 1
  • 1
  • 9