- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the types of variables a class can have in Java?
There are three different types of variables a class can have in Java are local variables, instance variables, and class/static variables.
Local Variable
A local variable in Java can be declared locally in methods, code blocks, and constructors. When the program control enters the methods, code blocks, and constructors then the local variables are created and when the program control leaves the methods, code blocks, and constructors then the local variables are destroyed. A local variable must be initialized with some value.
Example
public class LocalVariableTest { public void show() { int num = 100; // local variable System.out.println("The number is : " + num); } public static void main(String args[]) { LocalVariableTest test = new LocalVariableTest(); test.show(); } }
Output
The number is : 100
Instance Variable
An instance variable in Java can be declared outside a block, method or constructor but inside a class. These variables are created when the class object is created and destroyed when the class object is destroyed.
Example
public class InstanceVariableTest { int num; // instance variable InstanceVariableTest(int n) { num = n; } public void show() { System.out.println("The number is: " + num); } public static void main(String args[]) { InstanceVariableTest test = new InstanceVariableTest(75); test.show(); } }
Output
The number is : 75
Static/Class Variable
A static/class variable can be defined using the static keyword. These variables are declared inside a class but outside a method and code block. A class/static variable can be created at the start of the program and destroyed at the end of the program.
Example
public class StaticVaribleTest { int num; static int count; // static variable StaticVaribleTest(int n) { num = n; count ++; } public void show() { System.out.println("The number is: " + num); } public static void main(String args[]) { StaticVaribleTest test1 = new StaticVaribleTest(75); test1.show(); StaticVaribleTest test2 = new StaticVaribleTest(90); test2.show(); System.out.println("The total objects of a class created are: " + count); } }
Output
The number is: 75 The number is: 90 The total objects of a class created are: 2
- Related Articles
- What are class variables, instance variables and local variables in Java?
- What are variables and types of variables in C++?
- What are the member variables of a class in C#?
- What are the Types of Credit Policy Variables?
- What is the difference between class variables and instance variables in Java?
- Can a method local inner class access the local final variables in Java?
- Are the private variables and private methods of a parent class inherited by the child class in Java?
- Can an anonymous class have constructors in Java?
- Can we have variables and methods in an enum in Java?
- How many ways are there to initialize the instance variables of a class in java?
- What are new methods have added to the Arrays class in Java 9?
- What are the types of arrays in Java?
- Java variables and data types
- Class and Static Variables in Java
- What are the different types of keywords in Java?
