Difference between static and non-static variables in Java Last Updated : 11 Apr, 2023 Comments Improve Suggest changes Like Article Like Report There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. Important points for static variables :-We can create static variables at class level only. See herestatic block and static variables are executed in the order they are present in a program. Java // Java program to demonstrate execution // of static blocks and variables class Test { // static variable static int a = m1(); // static block static { System.out.println("Inside static block"); } // static method static int m1() { System.out.println("from m1"); return 20; } // static method(main !!) public static void main(String[] args) { System.out.println("Value of a : " + a); System.out.println("from main"); } } //By Jatin Sharma Output : 14 Output : from m1 Inside static block Value of a : 20 from mainNon-Static VariableLocal Variables: A variable defined within a block or method or constructor is called local variable.These variables 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 this variable only within that block.Initialisation of Local Variable is Mandatory. Java public class Student { public static void main(String[] args) { // if we try to call non-static method without using instance from static method it will get error || Non-static method 'getName()' cannot be referenced from a static context // getName(); Student student = new Student(); student.greet("Knoldus"); } public void greet(String name){ System.out.println("Hello " + name); } } //by Jatin Sharma Output : x : 1, y: 10 x : 100, y: 10 x : 1000, y: 2000 Output : Hello KnoldusInstance 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.Initialisation of Instance Variable is not Mandatory. Its default value is 0Instance Variable can be accessed only by creating objects. Java public class Studentsrecords { /* declaration of instance variables. */ public String name; //public instance String division; //default instance private int age; //private instance /* Constructor that initialize an instance variable. */ public Studentsrecords(String sname) { name = sname; } /* Method to initialize an instance variable. */ public void setDiv(String sdiv) { division = sdiv; } /* Method to initialize an instance variable. */ public void setAge(int sage) { age = sage; } /* Method to display the values of instance variables. */ public void printstud() { System.out.println("Student Name: " + name ); System.out.println("Student Division: " + division); System.out.println("Student Age: " + age); } /* Driver Code */ public static void main(String args[]) { Studentsrecords s = new Studentsrecords("Monica"); s.setAge(14); s.setDiv("B"); s.printstud(); } } //by jatin sharma Output : Student Name: Monica Student Division: B Student Age: 14 The main differences between static and non static variables are: Static variableNon static variableStatic variables can be accessed using class nameNon static variables can be accessed using instance of a classStatic variables can be accessed by static and non static methodsNon static variables cannot be accessed inside a static method.Static variables reduce the amount of memory used by a program.Non static variables do not reduce the amount of memory used by a programIn Static variable Memory is allocated only once, at the time of class loading.In non Static variable Memory is allocated each time an instance of the class is created.Static variables Can be accessed from any part of the program.Non Static variables Can be accessed only within the class or its instance.Static variables Exists for the entire lifetime of the program.Non Static variables Exists for the lifetime of the object.Static variables Default value is assigned automatically.Non Static variables Default value is not assigned automatically.Static variables are shared among all instances of a class.Non static variables are specific to that instance of a class.Static variable is like a global variable and is available to all methods.Non static variable is like a local variable and they can be accessed through only instance of a class. Comment More infoAdvertise with us Next Article Difference between static and non-static variables in Java bestharadhakrishna Follow Improve Article Tags : Java Difference Between Static Keyword Practice Tags : Java Similar Reads Difference between static and non-static method in Java A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access 6 min read Static and Non Static Blank Final Variables in Java In Java, a variable provides us with named storage that our programs can manipulate. In Java, a blank final variable is a final variable that is declared but not immediately initialized. It must be assigned exactly once before use. When a variable is declared with static, it becomes a static blank f 6 min read Difference Between Static and Non Static Nested Class in Java Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between makin 4 min read Difference between volatile and transient keywords in Java Just like any other programming language, Java has a set of keywords which are reserved and have a special meaning. In this article, we will see the difference between the keywords volatile and transient. Before getting into the differences, let us first understand what each of them actually means. 3 min read Difference between Instance Variable and Class Variable Instance Variable: It is basically a class variable without a static modifier and is usually shared by all class instances. Across different objects, these variables can have different values. They are tied to a particular object instance of the class, therefore, the contents of an instance variable 2 min read Difference Between Static and Const in JavaScript Static variable: A static variable in JavaScript is basically a property of the class which is not used on the object of the class but is used in the class itself. This static variable is stored into the data segment of the memory and its value is shared among all the objects/instances created in th 3 min read Difference Between Singleton Pattern and Static Class in Java Singleton Pattern belongs to Creational type pattern. As the name suggests, the creational design type deals with object creation mechanisms. Basically, to simplify this, creational pattern explains to us the creation of objects in a manner suitable to a given situation. Singleton design pattern is 6 min read Difference Between Object and Instance in Java The object is an instance of a class. A class is like a blueprint or template that defines the properties and behavior of objects. When we create an object we are creating an instance of that class. Object in JavaThe object is an instance of a class. A class is a blueprint or template that describes 3 min read Difference between Local Variable and Global variable Local variables are declared within a specific block of code, such as a function or method, and have limited scope and lifetime, existing only within that block. Global variables, on the other hand, are declared outside of any function and can be accessed from any part of the program, persisting thr 2 min read Are Static Local Variables Allowed in Java? In Java, there are different types of variables, each with its own behavior and scope. Understanding these variables plays a very important role. In this article, we will discuss the concept of static with local variables, and we will also discuss in Java static local variables are allowed or not.Wh 3 min read Like