Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Field toString() method in Java with Examples
Next article icon

Static Method in Java With Examples

Last Updated : 02 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the static keyword is used to create methods that belongs to the class rather than any specific instance of the class. Any method that uses the static keyword is referred to as a static method.

Features of Static Method:

  • A static method in Java is associated with the class, not with any object or instance.
  • It can be accessed by all instances of the class, but it does not rely on any specific instance.
  • Static methods can access static variables directly without the need for an object.
  • They cannot access non-static variables (instance) or methods directly.
  • Static methods can be accessed directly in both static and non-static contexts.

Syntax to Declare a Static Method

access_modifier static return_type methodName() {

// method body

}

The name of the class can be used to invoke or access static methods.

Syntax to Call a Static Method

ClassName.methodName();

Example 1: Static Method Cannot Access Instance Variables

The JVM executes the static method first, even before creating class objects. So, static methods cannot access instance variables, as no object exists at that point.

Java
// Java program to demonstrate that // the static method does not have // access to the instance variable import java.io.*;  public class Geeks {          // static variable     static int a = 40;      // instance variable     int b = 50;      void simpleDisplay()     {         System.out.println(a);         System.out.println(b);     }      // Declaration of a static method     static void staticDisplay()     {        System.out.println(a);      }      // main method     public static void main(String[] args)     {         Geeks obj = new Geeks();         obj.simpleDisplay();          // Calling static method         staticDisplay();     } } 

Output
40 50 40 


Example 2: Static Methods Accessed from Both Static and Non-Static Methods

Java
// Java program to demonstrate that // in both static and non-static methods, // static methods are directly accessed import java.io.*;  public class Geeks {        static int num = 100;     static String str = "GeeksForGeeks";      // This is Static method     static void display()     {         System.out.println("Static number is: " + num);         System.out.println("Static string is: " + str);     }      // non-static method     void nonstatic()     {         // our static method can accessed          // in non static method         display();     }      // main method     public static void main(String args[])     {         Geeks obj = new Geeks();                // This is object to call non static method         obj.nonstatic();                // static method can called          // directly without an object         display();     } } 

Output
Static number is: 100 Static string is: GeeksForGeeks Static number is: 100 Static string is: GeeksForGeeks 

Why Use Static Methods?

  • To access or modify static variables or perform actions not tied to any instance.
  • Useful for utility or helper classes like Collections, Math, etc.

Restrictions on Static Methods

  • Non-static data members or non-static methods cannot be used by static methods, and static methods cannot call non-static methods directly.
  • In a static environment, this and super are not allowed to be used.

Why is the main Method Static in Java?

The main method must be static because the JVM does not create an object of the class before invoking it. If it were a non-static method, JVM would first build an object before calling the main() method, resulting in an extra memory allocation difficulty.

Difference Between the Static Method and Instance Method

Instance Methods

Static Methods

It requires an object of the class.It does not require an object of the class.
It can access all attributes of a class.It can access only the static attribute of a class.
The methods can be accessed only using object reference.The method is only accessed by class name.
Syntax: Objref.methodname()Syntax: className.methodname()
It's an example of pass-by-value programming.It is an example of pass-by-reference programming.

Next Article
Field toString() method in Java with Examples

S

sanketnagare
Improve
Article Tags :
  • Java
  • Java-Object Oriented
Practice Tags :
  • Java

Similar Reads

  • Field set() method in Java with Examples
    The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s
    4 min read
  • Optional of() method in Java with examples
    The of() method of java.util.Optional class in Java is used to get an instance of this Optional class with the specified value of the specified type. Syntax: public static <T> Optional<T> of(T value) Parameters: This method accepts value as parameter of type T to create an Optional insta
    1 min read
  • Year toString() method in Java with Examples
    The toString() method of Year class in Java is used to return the string representation of this Year object. Syntax: public String toString() Parameter: This method does not accepts any parameter. Return Value: It returns the string representation of this Year object. Below programs illustrate the t
    1 min read
  • Field toString() method in Java with Examples
    The toString() method of java.lang.reflect.Field is used to get a string describing this Field. The format of the returned string is the access modifiers for the field, if any, followed by the field type, followed by a space, followed by the fully-qualified name of the class declaring the field, fol
    3 min read
  • Year now() method in Java with examples
    The now() method of Year class in Java is used to return the current year from the system clock in the default time-zone. Syntax: public static Year now() or, public static Year now(Clock clock) or, public static Year now(ZoneId zone) Parameter: The parameter is optional for this method as shown in
    2 min read
  • ArrayList size() Method in Java with Examples
    In Java, the size() method is used to get the number of elements in an ArrayList. Example 1: Here, we will use the size() method to get the number of elements in an ArrayList of integers. [GFGTABS] Java // Java program to demonstrate the use of // size() method for Integer ArrayList import java.util
    2 min read
  • util.date class methods in Java with Examples
    Following are some important date class methods : .toString() : java.util.Date.tostring() method is a java.util.Date class method.It displays the Current date and time. Here Date object is converted to a string and represented as: day mon dd hh:mm:ss zz yyyy day : day of the week mon : month dd : da
    5 min read
  • Abstract Method in Java with Examples
    In Java, Sometimes we require just method declaration in super-classes. This can be achieved by specifying the Java abstract type modifier. Abstraction can be achieved using abstract class and abstract methods. In this article, we will learn about Java Abstract Method. Java Abstract MethodThe abstra
    6 min read
  • List add() Method in Java with Examples
    The List add() method adds (appends) an element to a list. It can be used for both ArrayList and LinkedList. Example of List add() Method: Java Code // Java program to demonstrate // ArrayList usage import java.io.*; import java.util.ArrayList; import java.util.List; class GFG { public static void m
    3 min read
  • Field get() method in Java with Examples
    The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences