Reputation:
I want to ask you if a variable declared inside main method is an instance variable or a local variable.
This is the code:
public class App {
public static void main(String[] args) {
Animal fish = new Fish();
...
}
}
I'm watching a video tutorial from Udemy and the instructor say that fish
is an instance variable. I thought that a variable declared inside a method is a local variable.
Upvotes: 0
Views: 9486
Reputation: 315
Your fish
variable is a reference variable
. Reference variables are those variables which hold the memory reference to an Object (in your case, object of Fish
class). Instance variables
are those variables which belong to an instance. In your Fish class, you may have declared some variables that define the property of the object. Eg: color
may be a variable that defines the color of a particular fish(an instance of Fish class). Now for your answer, the fish
variable is a local variable as it is present inside your main method which is just another method. So, if you want to access the same object in some other method, you need to pass the reference of that particular object as an argument to that method like this:
public class App {
public static void main(String[] args) {
Animal fish = new Fish();
...
new App().anotherMethod(fish) //pass the memory reference of the object
}
public void anotherMethod(Fish fishRef){ // memory reference is now in fishRef
//fishRef now accesses the same object as your fish variable in main method
}
}
Upvotes: 0
Reputation: 63
It is local variable due to its visibility only in the main method whereas instance variables can been seen by all methods in the class.
you may refer this old thread to tell the difference between local variables and instance variables
Upvotes: 0
Reputation: 6123
Oracle defines "instance variable" thus:
Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class
I think the Udemy tutor is referring to "variables that reference an instance", using an unfortunate terminology. In other words, he's using the term "instance variable" colloquially rather than with its official meaning.
Upvotes: 0
Reputation: 3
This is called by Scope.
Method body scope Variable is accessible in method body only (local variables, parameters)
Upvotes: 0
Reputation: 5859
Variables declared inside the class but outside the body of the method are called instance variables. It is called instance variable because its value is instance specific and is not shared among instances.
Variables declared inside the body of a method are called local variables. You can use this variable only within that method and they are not seen outside of that method.
class A {
int data = 50; //instance variable
void method() {
int n = 90; //local variable
}
Upvotes: 2
Reputation: 678
If you declare any variable within any method, it is a local variable. Your main might be a special method, but it is a method. Therefore, anything you declare in your main will be a local variable as well.
Upvotes: 0