Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Variables in Java

  • Rabindranath Tagore Biography in Hindi, Poems, Birthday
    Rabindranath Tagore Biography in Hindi, Title of Gurudev Biography
  • America Independence Day : 4th July USA | USA Birthday
    America Independence Day : 4th July USA | USA Birthday General Knowledge
  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
    MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi Biography
  • Diwali The Festival of Lights
    Diwali 2022, Indian Festival of Lights essay (दिवाली त्योहार पर निबंध, कहानी) Festival
  • Nelson Mandela Biography in Hindi | Nelson Mandela Day
    Nelson Mandela Biography in Hindi | Nelson Mandela Day Biography
  • P V Sindhu Biography in Hindi
    P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति Biography
  • Thomas Edison Biography in Hindi - थॉमस एडिसन जीवनी
    Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी Biography
  • What is Pronoun?
    What is Pronoun with example? Pronoun definition and examples Grammar

Variables in Java

Posted on August 13, 2021October 11, 2021 By GeekCer Education
Variables in Java

Variables in Java is the name you give to the memory area that holds the value during the execution of the Java program. You can use both the “field” as well as “Variable” in the in the programming language.

A program generally acts on data, processes it, and provides the results. So data and data types are very important concept of a program.

You must specify the type and need to assign the value to create a variable in Java.

Table of Contents

  • Types of Variables based on Data Types
  • Types of Variables based on positions
    • Primitive Variables in Java
    • Reference Variables in Java
    • Instance Variables or Non-Static Fields
    • Class Variables or Static Fields
    • Local Variables in Java

Types of Variables based on Data Types

There are two type of variables are available based on data types:

  • Primitive Variables
  • Reference Variables

Types of Variables based on positions

There are three type of variables are available based on positions:

  • Instance Variables (Non-Static Fields)
  • Class Variables (Static Fields)
  • Local Variables

Primitive Variables in Java

Primitive Variable represents the primitive values. For example, You declare a variable with byte, short, int, long etc.

 
int x = 10;

Reference Variables in Java

Reference Variable refers an object of a class when you need to keep an object to variable.

 
Employee emp = new Employee();

Instance Variables or Non-Static Fields

The value of the instance or no-static variable varies from object to object that’s the reason the value of such type of variable are unique to each instance of a class.


public class Example {  // Class Name
 private int instanceVar = 20; // Instance Variable
 public static void main(String[] args) {

 }
}

Class Variables or Static Fields

The value of the class variable or static field doesn’t vary from object to object. We declare a static variable or class variable with the static modifier. No matter how many times the class has been instantiated there will be exactly one copy of this variable.


public class Example {  // Class Name
 private static staticVar = 20; // Static Variable
 public static void main(String[] args) {

 }
}

Local Variables in Java

The local variables are only visible to the methods in which we declare because we declare a variable inside the method only, they are not accessible from the rest of the class.

  • Automatic variables or Stack Variables or temporary variables are the terminology you can user other than local variables.
  • You must declare before using it because for local variable JVM doesn’t provide any default value.
  • Compulsory we should perform initialization explicitly before using local variable.
  • You can only use “final” modifier for the local variable otherwise you will get compile timer error.

public class Example {  // Class Name
 void display() {
  int localVar = 20;

 }
}

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java Tags:Data Types, Fields, Local Variable, Primitive Type, Variables

Post navigation

Previous Post: Java Comments : Types of java comments, Syntax & Examples
Next Post: Data Types in Java

More Related Articles

Java Vector Vector in Java Collection, Constructors, Example Programs Java
Java Keywords Java Keywords Java
Java 8 interview questions and answers, Java Stream, Optional Java 8 interview questions and answers, Java Stream, Optional Important
Method Overloading in Java Method Overloading in Java Java
Java ArrayList Java ArrayList Java
Difference between Runnable and Callable in Java Difference between Runnable and Callable in Java Differences
  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved