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

  • Waterfall Model in Hindi (Waterfall Model क्या है?)
    Waterfall Model in Hindi (Waterfall Model क्या है?) Important
  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar
  • Git commands with example and git log, git bash, create repo
    Git commands with example and git log, git bash, create repo Important
  • Linux Commands With Example | Linux commands cheat sheet
    Linux Commands With Example | Linux commands cheat sheet Important
  • Difference between TCP and UDP
    Difference between TCP and UDP Differences
  • Jenkins java | Installing Jenkins on Windows
    Jenkins java | Installing Jenkins on Windows Important
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय)
    Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय) Biography

Variables in Java

Posted on August 13, 2021October 11, 2021 By admin
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

Object Oriented Programming Object Oriented Programming Java
Loop in Java Loop in Java Java
Java Learning Tutorial Java Tutorial for Advanced Learning, Buzzwords, Parts of Java Java
Stack Vs Heap in Java Stack Vs Heap in Java | Memory Allocation Section in Java Differences
Difference between final, finally and finalize Difference between final, finally and finalize Differences
Difference between Interface and Abstract class Difference between Interface and Abstract class Differences

Related Posts

  • Object Oriented Programming
    Object Oriented Programming Java
  • Loop in Java
    Loop in Java Java
  • Java Learning Tutorial
    Java Tutorial for Advanced Learning, Buzzwords, Parts of Java Java
  • Stack Vs Heap in Java
    Stack Vs Heap in Java | Memory Allocation Section in Java Differences
  • Difference between final, finally and finalize
    Difference between final, finally and finalize Differences
  • Difference between Interface and Abstract class
    Difference between Interface and Abstract class 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

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • Waterfall Model in Hindi (Waterfall Model क्या है?)
    Waterfall Model in Hindi (Waterfall Model क्या है?) Important
  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar
  • Git commands with example and git log, git bash, create repo
    Git commands with example and git log, git bash, create repo Important
  • Linux Commands With Example | Linux commands cheat sheet
    Linux Commands With Example | Linux commands cheat sheet Important
  • Difference between TCP and UDP
    Difference between TCP and UDP Differences
  • Jenkins java | Installing Jenkins on Windows
    Jenkins java | Installing Jenkins on Windows Important
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय)
    Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय) Biography
  • 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