Types of Variables in Java27 Mar 2025 | 3 min read In Java, variables are containers that hold values. A variable name represents the memory location name. Each variable consists of three elements: data type, variable name, and value. A variable may have a scope (private, protected), but it depends on the requirement.
Example: int price=90; Basically, there are three types of variables in Java.
![]() 1. Local VariablesLocal variables are defined inside blocks, constructors, or methods. When control leaves these variables, they are destroyed. Upon entering the method, constructor, or block, these variables are generated. Local variables need to be initialized before use because they do not have default values. Key Features of Local Variables
Example of Local VariableExampleCompile and RunOutput: 10 2. Instance VariablesAn instance variable is a variable that is declared inside a class but outside the body of the method or constructor, or block. It is called an instance variable because its value is instance-specific and is not shared among instances. These variables are specific to an object of the class. It means that each object has its own copy of the instance variable. It is not declared as static. Key Features of Instance Variables
Example Instance VariablesExampleOutput: Employee's Name is: Andrew Age: 19 3. Static VariableA variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory. Key Features of Static Variables
Example of Static VariablesExampleCompile and RunOutput: Employee-1 salary is: 24000 Employee-2 salary is: 24000 Next TopicAdvantages and disadvantages of Java |
In Java, List is is an interface of the Collection framework. It provides us to maintain the ordered collection of objects. The implementation classes of List interface are ArrayList, LinkedList, Stack, and Vector. The ArrayList and LinkedList are widely used in Java. In this section, we...
4 min read
? In Java, overriding and overloading are the two most important features of object-oriented programming. The feature is used when the programmer wants to achieve polymorphism. The question, can we override static method in Java or can you overload static method in Java are the two most...
3 min read
JFileChooser is a class that is present in the java Swing package. The java Swing package is essential for JavaTM Foundation Classes(JFC). JFileChooser contains many elements that assist in building a graphical user Interface in java. Java Swing gives components like buttons, panels, dialogs, etc. JFileChooser...
5 min read
Generic is used for creating Java code for graphs. Java's HashMap class is used to implement the Graph class. As we know, HashMap has a key and a value; in the graph, nodes are represented as keys, and their adjacency is listed as values. What is Generic? Generics...
9 min read
Like C++, Java also supports the copy constructor. But in C++, it is created by default. In Java, we define copy constructors ourselves. Constructor In Java, a constructor is the same as a method, but the only difference is that the constructor has the same name as...
10 min read
An integer count is given to us associated with a string 'str' made up of lowercase English letters. The objective behind this particular problem is to find "equal count substrings." When every distinct letter appears within a substring exactly count times, the substring is said...
6 min read
The Largest Square Matrix Problem involves finding the size of the largest square sub-matrix within a given binary matrix, where all the elements of the sub-matrix are 1s. It is a classic problem of using dynamic programming to solve a two-dimensional problem efficiently. In Java, the...
10 min read
In the world of software development, managing tasks and orchestrating workflows efficiently is crucial for the success of any application. One common challenge developers face is scheduling and executing jobs at specific intervals. In this section, we will explore the design and implementation of a job...
6 min read
C++ supports the scope resolution operator (::) that allows us to resolve the ambiguous call or reference to identifiers. Like C++, Java does not support the scope resolution operator. Java uses the same operator (::) but with different names. The scope resolution operator in Java...
3 min read
Find the greatest sum of a continuous subarray such that the subarray only consists of prime numbers given an array arr[] of integers of size n. Stated a certain way, no non-prime integer is allowed to exist in the selected subarray. Example 1: Input: int a[] = {...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India