Types of variables in java

Real-world objects around us are all defined by their properties. It’s one of the well-defined unique ways to look at our world. Now java being an object-oriented language is based on this abstract concept of objects and their properties. For defining the properties of objects we need to use the variables and the classes are defined using objects. What makes a red ball different from a blue ball? Well, primarily it’s a color property if we don’t investigate the details.

Now let us take a closer look at local, instance, and static variables in a java programming language. Now, this can be easily confused with the different types of variables in java like int, float, char, etc. This concept is also different from the scope of variables in java which specifies where variables can be accessed from.

Types Of Variables In Java

1. Local variables:

The authority for these variables is present only within the class in which it is declared. Local variables can also be created within the parameters of the method and be given a value when calling the method. They are not assigned a default value on creation so they must be given a value by the program. When we look at the memory structure for a java program local variables reside in the stack of the program and are discarded when the program exits the block it was declared in.

2. Instance variables:

Instance variables are declare outside of all methods in a class and are available to all methods within its scope. A variable’s scope is define using private, public, or protected. Java also has a default scope when the scope is not specified.

These are in general the properties of the class which usually differ for every instance inside the class. For example, if we have a class for a ball the color property can be red for the first instance and blue for the second instance. On initialization, they reside within the heap of the program memory.

3. Static variables:

These are declare like an instance variable but with a static keyword. The static keyword means the variable is the same across instances. These are used for fixed properties of the class and are usually declare using the final keyword. These are those variables that can be accessed without even the need for creating an instance for the class we are working in. 

Let us look at an example to understand this in a much simpler way.  Say we would like to create a fantasy cricket application. We would require a team class with different properties such as team name, players, points, rating the number of players in the team, etc. To keep it simple I have only considered the name of the team and rating.

We have two instance variables and a constructor to initialize those variables. The instance variables have been declare as private so they can only be access and modified using getters and setters. Programmers are often interests in this because of security reasons. The variables in the parameter of the constructor are local variables that are assign a value on instance creation. Static variables can also be declare by using the final keyword. This means all instances of teams will have a player count of 15.

The above code example initializes a new team instance with the values “CSK” and 23. We can also see the static value being refer without the object as discuss earlier. This will print 15 to the console.

In conclusion, variable in Java types are the building blocks of the object-oriented concept in java. It is crucial to have a solid understanding of this concept before progressing to more complex topics within java.

written by: K.U Minnakan Seral

Reviewed By: M SASANK SAI, Bujagawni Sai Teja Goud

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

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