Welcome back to Java Code Online. While working with Java, you must have multiple times come across words like instance variable and local variable. But have you ever wondered that what is the difference between them. Well, today I am going to discuss that. But before proceeding on this topic you need to know what is an instance variable and what is a local variable in Java. Refer to my previous post for information on the instance variable and local variable in Java.
There are two major differences between an instance variable and a local variable in Java. They are:-
1. Instance variable is declared within a class but not within a method, while a local variable is declared within a method.
2. Instance variable get a default value, so if they are used before initializing them, then also they compile fine. But in the case of local variable in Java, local variable in Java do not get an initial value, so if you use them in your code without initializing them, then you will get a compile time exception.
I hope the article was helpful in understanding the difference between instance variable and local variable in Java. If you liked the article, then do leave a comment. For more information on Java keep buzzing Java Code Online.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.