BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Difference between static and non-static members in Java

Last Updated: June 2, 2024 by Chaitanya Singh | Filed Under: java

Java is a Object Oriented Programming(OOP) language, which is often interpreted that we need objects to access methods and variables of a class, however this is not always true. While discussing static keyword in java, we learned that static members are class level and can be accessed directly without creating any instance. In this article we will discuss the difference between static and non-static members in Java.

Static Members

1. Static Fields (Variables)

  • Definition: Static variables are class-level variables, which means they are shared by all instances of the class. For example, if a class has two instances obj1 and obj2, they both access to the same static variable.
  • Memory Allocation: The memory is allocated to static variables when the class is loaded by the JVM.
  • Access: Since, they are same for all instances, they can be called using class name without the need of any instances.
  • Initialization: Initialised during memory allocation, i.e. when the class is first loaded.

Static variables Example

class MyClass {
//static variable, note the static keyword before int
static int staticVariable = 10;
}

public class Test {
public static void main(String[] args) {
// Accessed using class name, we did not create an
// instance of class as it is not needed
System.out.println(MyClass.staticVariable);
}
}

2. Static Methods

  • Definition: Similar to static variables, static methods belong to class rather than any specific instance of class.
  • Access: Can be accessed using class name, as shown in the following example.
  • Limitations: This is very important to note, static methods cannot access non static variables as well as non static methods. This is due to the underlying nature that static methods are not tied to any instances.

Static Methods Example

class MyClass {
// Note the static keyword before return type of method
static void staticMethod() {
System.out.println("This is a Static method");
}
}

public class Test {
public static void main(String[] args) {
// Accessed using class name
MyClass.staticMethod();
}
}

Non-Static Members

1. Instance Fields (Variables)

  • Definition: These variables are tied to the instances of the class.
  • Memory Allocation: Memory is allocated when the new object of the class is created.
  • Access: These are accessed using instances of class as shown in the example below.
  • Initialization: They are initialized when the object is instantiated.

Instance Variables Example

class MyClass {
// Instance variable, there is no static keyword here
int instanceVariable = 20;
}

public class Test {
public static void main(String[] args) {
// We have to create an object of the class to access
// instance variable
MyClass obj = new MyClass();

// Accessed using object
System.out.println(obj.instanceVariable);
}
}

2. Instance Methods

  • Definition: These methods are tied to the instances of the class.
  • Access: These are accessed using instances of class.
  • Capabilities: Unlike static methods which cannot access non-static variables and methods, these methods can access both instance variables and static variables.

Instance Methods Example

class MyClass {
// non-static variable
int instanceVariable = 20;

//non-static method
void instanceMethod() {
System.out.println("Instance method called");
}
}

public class Test {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.instanceMethod();
}
}

Key Differences between static and non-static members

  1. Tied:
    • Static Members: Belong to the class.
    • Non-Static Members: Belong to instances (objects) of the class.
  2. Access:
    • Static Members: Can be accessed directly using class name without creating any object.
    • Non-Static Members: Can be accessed using object of the class.
  3. Memory Allocation:
    • Static Members: Memory allocation is done when the class is loaded.
    • Non-Static Members: Memory allocation is done each time an object is created.
  4. Initialization:
    • Static Members: Initialization is done when the class is loaded by JVM.
    • Non-Static Members: Initialization is done every time a new object is created.
  5. Scope:
    • Static Members: Class level scope.
    • Non-Static Members: Scope is limited to the instance level.
  6. Access to Instance Members:
    • Static Methods: Cannot access instance members directly.
    • Instance Methods: Can access both instance and static members.

Static vs non-static Example Program

class Example {
// Static variable
static int staticVariable = 10;
// Instance variable
int instanceVariable = 20;

// Static method
static void staticMethod() {
System.out.println("Static method");
// Can access static variables
System.out.println(staticVariable);
// Error: Cannot access instance variables, if you
// un-comment the following statement, it will throw error
// System.out.println(instanceVariable);
}

// Non-static (Instance) method
void instanceMethod() {
System.out.println("Instance method");

// Can access static variables
System.out.println(staticVariable);
// Can access non-static variables
System.out.println(instanceVariable);
}
}

public class Test {
public static void main(String[] args) {
// Calling static method using class name
Example.staticMethod();

Example obj = new Example();
// Calling non-static(instance) method using object
obj.instanceMethod();
}
}

Top Related Articles:

  1. Difference between Static and Dynamic Dispatch in Java
  2. Static and dynamic binding in java
  3. What is private Keyword in Java
  4. Constructor Overloading in Java with examples
  5. Java – Static Class, Block, Methods and Variables

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Prasannaraj says

    December 16, 2014 at 8:03 AM

    Hi,

    Why can’t we use in last example?
    public static void main(String args[])
    {
    System.out.println(“Age is:”+ getAge(15));
    }

    Reply
    • praveen says

      December 26, 2014 at 5:44 AM

      Because to access any non static members we have to first create object of that class(Here Sample is class), and then access the methods.
      Now,
      Public static void main()
      {
      Sample obj= new Sample();
      Obj.setAge(30);
      System.output.println(“Age=”+obj.getAge());
      }

      Reply
  2. Poonam Marathe says

    January 19, 2015 at 5:39 PM

    where the static variables are get stored?and how they get processed?

    Reply
    • gaensh chowkekar says

      February 22, 2017 at 3:51 AM

      static variable are stored in class common memory

      Reply
    • meghamsh says

      May 4, 2017 at 1:30 PM

      The Static variables are stored in heap memory….basically we use static to define a fixed final variable because it stays constant through out the application…
      e.g. private static final float pi=3.14f;

      Reply
  3. akintunde says

    June 10, 2015 at 1:17 PM

    good day, please can you explain why the error when the age isn’t assigned to an object?

    Reply
    • laxman says

      January 30, 2016 at 2:41 AM

      like said earlier,it is because for the non static method to be used in static, an object should be created first and then called using that obj eg. obj.getage() in static method.
      hope it helps

      Reply
  4. Abhay jadhav says

    August 4, 2015 at 1:28 PM

    Nice stuff related to java
    Thank you it really helps

    Reply
  5. Haider Ali says

    March 8, 2017 at 4:46 PM

    println is a static funtion or non- static?
    plz discuss in detail.

    Reply
  6. Vidhisha says

    May 6, 2017 at 8:12 AM

    Hi,

    Can you please give example of static methods in terms of real time application.Where ,how and what is the purpose of using static methods.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap