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
}
}