Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Variables in Java

Variables in Java

Table of Contents

  • Variable in Java
      • What is a Variable?
    • Instance Variables in Java
    • Static Variables in Java
      • Additional points on static variable:
    • Local Variables in Java
      • Variable Scope in Java

Variables in Java

Variable in Java

In this Tutorial we will learn about Java Variables, various types of Variables along with code examples to understand Java Variables.

What is a Variable?

When we want to save any information, we save it in a Computer’s address. We give that address a name so we don’t have to remember the complicated address where we have our information stored. Variable refers to how an address is named. The name of the memory location is Variable.

In other words, Variable is a name which is used to store a value of any type during program Execution.

To declare the Variable in Java, we can use following syntax


datatype variableName;

Here, datatype refers to type of variable which can any like: int, float etc. and variableName can be any like: empId, amount, price etc.

Java Programming language defines mainly three kind of Variables.

  1. Instance Variables
  2. Static Variables (Class Variables)
  3. Local Variables

Instance Variables in Java

Variables that are Declared inside a class but outside of any methods, Constructors, or blocks are known as instance Variables. A Variable of an object known as a field or property is also known as an instance Variable. Object Variables are the name given to them. Since each object has a copy of each Variable, changing the value of a Variable in one object has no impact on the instance Variable.

class Student
{
    String name;
    int age;
}

Here name and age are instance variable of Student class.


Static Variables in Java

Class variables that are declared as static are called static. Static variables only need to be initialised once. Along with the final keyword, static variables are also used to declare constants.

class Student
{
    String name;
    int age;
    static int instituteCode=1101;
}

Here instituteCode is a static variable. Each object of Student class will share instituteCode property.

Additional points on static variable:

  • static variable are also known as class variable.
  • static means to remain constant.
  • In Java, it means that it will be constant for all the instances created for that class.
  • static variable need not be called from object.
  • It is called by classname.static_variable_name

Note: A static variable can never be defined inside a method i.e it can never be a local variable.

Example:

Consider creating two Student class objects and changing the value of a static variable in one of them. Printing it from another object will now print the updated value. This is due to the fact that it was declared static, meaning it applies to every new object.

package onlineexamguide;

class Student{
    int a;
    static int id = 35;

    void change(){

        System.out.println(id);
    }
}

public class onlineexamguide {
    public static void main(String[] args) {

        Student o1 = new Student();
        Student o2 = new Student();

        o1.change();

        Student.id = 1;
        o2.change();
    }
}

35 1


Local Variables in Java

In a block, constructor, or method, local variables are declared. Local variables are created at the beginning of a method, constructor, or block and destroyed at the end. Stack houses local variables. Local variables don’t use access modifiers.

float getDiscount(int price)
{
 float discount;
 discount=price*(20/100);
 return discount;
}

Here discount is a local variable.

Variable Scope in Java

Scope of a variable decides its accessibility throughout the program. As we have seen variables are different types so they have their own scope.

Local variable: Scope of local variable is limited to the block in which it is declared. For example, a variables declared inside a function will be accessible only within this function.

Instance variable: scope of instance variable depends on the access-modifiers (public, private, default). If variable is declared as private then it is accessible within class only.

And If variable is declared as public then it is accessible for all and throughout the application.

If variable is declared as default the it is accessible with in the same package.

For more details about accessibility you can refer our detailed tutorial.

Example:

So In this example, we created two variables a and i, first is declared inside the function and second is declared inside for loop. Both variables have their own scope in which they are declared, so accessing outside the block reports an error.


public class HelloWorld {

    public static void main(String[] args) {

        int a = 10;
        
        for(int i = 0; i<5; i++) {
            System.out.println(i);
        }
        
        System.out.println("a = "+a);
        System.out.println("i = "+i); // error

    }

}
    

error: cannot find symbol i

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.