Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. Static vs Non-static Keywords in Java: Everything You Need to Know

Static vs Non-static Keywords in Java: Everything You Need to Know

Date- Sep 01,2023

3356

java static keyword


In Java, the static keyword is used to define members (variables and methods) that belong to a class itself rather than to instances (objects) of the class. On the other hand, non-static (sometimes called "instance") members belong to individual instances of the class. Here are the key differences between static and non-static members in Java:

  1. 1. Ownership:

    • 1.staticbelongs to the class itself, shared by all instances of the class. There is only one copy of a static member in memory, regardless of how many instances of the class exist.
    • 2. Non-static: Belongs to individual instances of the class. Each instance has its own copy of non-static members.
  2. 2. Access:

    • 1.static: Can be accessed using the class name (e.g., ClassName.staticMember) or directly without an instance reference.
    • 2. Non-static: Requires an instance of the class to be created before accessing non-static members (e.g., object.nonStaticMember).
  3. 3. Memory Allocation:

    • 1. static: Allocated memory once when the class is loaded by the classloader.
    • 2. Non-static: Allocated memory separately for each instance when objects are created.
  4. 4. Lifecycle:

    • 1. static: Exists for the entire lifetime of the program or until the class is unloaded.
    • 2. Non-static: Exists as long as the instance to which it belongs exists and is eligible for garbage collection when the instance is no longer referenced.
  5. 5. Common Use Cases:

    • 1. static: Used for constants, utility methods, shared resources, and for defining class-level behavior that is not dependent on instance-specific data.
    • 2. Non-static: Used for instance-specific data and behavior that depends on the state of individual objects.

Here's an example illustrating the differences:


In this example, staticVar and staticMethod are shared among all instances of Blog02(i.e My Class), while nonStaticVar and nonStaticMethod are specific to each instance. Understanding when to use static and non-static members is crucial for designing and implementing effective Java classes.

package Tutorial_00;

public class Blog02 {
	 public static int staticVar = 0; // Static variable
	    
	    public int nonStaticVar = 0; // Non-static variable
	    
	    public static void staticMethod() {
	        // Static method
	        System.out.println("This is a static method.");
	    }
	  
    public void nonStaticMethod() {
        // Non-static method
        System.out.println("This is a non-static method.");
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Blog02.staticVar = 42; // Accessing static variable
		Blog02.staticMethod(); // Calling static method

		Blog02 obj1 = new Blog02();
		obj1.nonStaticVar = 10; // Accessing non-static variable
		obj1.nonStaticMethod(); // Calling non-static method

		Blog02 obj2 = new Blog02();
		obj2.nonStaticVar = 20;
		obj2.nonStaticMethod();
		
		}
	
	
	}



S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Access Modifiers in Java
Aug 24, 2023
Ultimate Guide to Method Overloading in Java with Examples
Dec 09, 2023
Parameterised constructor in java
Sep 09, 2023
Default constructor in java
Sep 07, 2023
Previous in Java
NoSuchwindowException in Java
Next in Java
Upcast and Downcast in Java

Comments

Contents

🎯

Interview Prep

Ace your Java interview with curated Q&As for all levels.

View Java Interview Q&As

More in Java

  • User-defined data types in java 6191 views
  • Master Java Type Casting: The Complete Guide with Examples 6180 views
  • How to Import java.util.List in Eclipse: Step-by-Step Guide 5789 views
  • org.openqa.selenium.SessionNotCreatedException: session not … 5738 views
  • java.lang.IllegalStateException: The driver executable does … 5064 views
View all Java posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor