Variable Types in java

Variable is nothing but a name or keyword which occupies space in the memory. Variables are classified in 3 types.

  • Local Variable
  • Instance (or non-static) variable
  • Static or Class variables
  • Parameters or arguments

Local Variables: 

  1. Local variables can be declared in the method,Constructor or block.
  2. It has scope till the end of execution of method,Constructor or block i.e. it will be destroyed once exit the method,Constructor or block.
  3. It is mandatory to initialize them, otherwise it throws error at compile time.
  4. Access modifier can not be used with them.
Instance/Non-static Variables:

  1. These variables are declared inside the class, but outside the method,Constructor or block.
  2. These are available to all method,Constructor or block.
  3. Access modifier can be used with them.
  4. If not initialized, then default values will be assigned to them like false for boolean, 0 for integer,null for object reference. Execute the below program for understanding.
Static Variables: 

  1. Static  keyword is used to define the static variables.
  2. These are declared at the class level.
  3. It can not be declared as local i.e. not inside a method, constructor or a block.
  4. It can be accessed with the class name i.e. classname.staticvariable.
Parameters or arguments: These are the variables which are passed as arguments in method. These variables can be used anywhere in the method and access memory during execution of methods. As you can see in above program , main method used args which is a parameter or argument.

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’. Select the desired category and post your question.
Print Friendly, PDF & Email
Difference between JVM,JDK and JRE
Data types in Java
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *