Introduction to Java Variables with Examples

In this article we are going to have Introduction to Java Variables. also we are going to create some Practical Examples in Java.

 

 

What is Variable in Java?

In Java a variable is a named storage location in the computer’s memory where data can be stored and manipulated during program execution. Variables have a specific data type that determines the type of data they can hold (like integer, floating-point number, character, boolean, etc.).

 

 

What are the Types of Java Variables ?

There are three different Java Variables types

 

1:  Java Local Variable : when we declare a variable inside the body of a method, that is called Local Variable.

you can use the variable only within that method.

Other methods in the class aren’t even aware that the variable exists.

  • These variable are created when the block in entered or the function is called and destroyed after exiting from the block or when the call returns from the function.
  • The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variable only within that block.
  • Initialization of Local Variable is Mandatory.

 

Example for Java local variable

 

 

 

 

This is the result

Introduction to Java Variables with Examples
Introduction to Java Variables with Examples

 

 

 

2: Java Instance Variables:  Instance variables are non-static variables and are declared in a class outside any method, constructor or block.

  • As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
  • Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
  • Initialization of Instance Variable is not Mandatory. Its default value is 0
  • Instance Variable can be accessed only by creating objects.

 

 

This will be the result

Java Instance Variable Example
Java Instance Variable Example

 

 

 

3: Java Static Variable:  Static variables are also known as Class variables.

  • These variables are declared similarly as instance variables, the difference is that static variables are declared using the static keyword within a class outside any method constructor or block.
  • Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create.
  • Static variables are created at start of program execution and destroyed automatically when execution ends.
  • Initialization of Static Variable is not Mandatory. Its default value is 0
  • If we access the static variable like Instance variable (through object), compiler will show the warning message and it wont hault the program. Compiler will replace the object name to class name automatically.
  • If we access the static variable without classname, Compiler will automatically append the class name.

 

 

 

This will be the result

Java Static Variable Example
Java Static Variable Example

 

 

 

 

FAQs:

 

What are variables in Java with examples?

In Java, variables are named storage locations in memory, and it is used to hold data. They have specific data types that determine the type of data they can store. These are examples of variables in Java:

 

 

 

What are the 8 types of variables in Java?

In Java, variables can be classified into eight types based on their scope and where they are declared:

  1. Local variables
  2. Instance variables (non-static fields)
  3. Class variables (static fields)
  4. Parameters (method parameters)
  5. Array elements
  6. Instance constants (final non-static fields)
  7. Class constants (final static fields)
  8. Exception parameters

 

 

 

What are the rules for variables in Java?

These are rules for variables in Java:

  • Variable names must begin with a letter (a-z or A-Z), dollar sign ($) or underscore (_) and can be followed by letters, digits, dollar signs or underscores.
  • Variable names are case-sensitive.
  • Java keywords cannot be used as variable names.
  • Variable names cannot contain spaces or special characters (except dollar signs and underscores).
  • Variable names should be descriptive and meaningful to enhance code readability.
  • Variable names should follow the camelCase convention for multi-word names, starting with a lowercase letter and capitalizing each subsequent word.

 

 

How to initialize a variable in Java example?

Variables in Java can be initialized (assigned an initial value) at the time of declaration or later in the code. This is an example of initializing a variable in Java:

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×