Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Java Tutorial

Java HOME Java Intro Java Get Started Java Syntax Java Output Java Comments Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Math Java Booleans Java If...Else Java Switch Java While Loop Java For Loop Java Break/Continue Java Arrays

Java Methods

Java Methods Java Method Parameters Java Method Overloading Java Scope Java Recursion

Java Classes

Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Constructors Java this Keyword Java Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java super Keyword Java Inner Classes Java Abstraction Java Interface Java Anonymous Java Enum Java User Input Java Date

Java Errors

Java Errors Java Debugging Java Exceptions Java Multiple Exceptions Java try-with-resources

Java File Handling

Java Files Java Create Files Java Write Files Java Read Files Java Delete Files

Java I/O Streams

Java I/O Streams Java FileInputStream Java FileOutputStream Java BufferedReader Java BufferedWriter

Java Data Structures

Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms

Java Advanced

Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting

Java Projects

Java Projects

Java How To's

Java How Tos

Java Reference

Java Reference Java Keywords Java String Methods Java Math Methods Java Output Methods Java Arrays Methods Java ArrayList Methods Java LinkedList Methods Java HashMap Methods Java Scanner Methods Java File Methods Java FileInputStream Java FileOutputStream Java BufferedReader Java BufferedWriter Java Iterator Methods Java Collections Methods Java System Methods Java Errors & Exceptions

Java Examples

Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate


Java Static


Java static Keyword

In the previous chapter, you learned a little about the static keyword when working with class attributes and methods. Now, let's go more in depth and see how static really works.

Static makes attributes and methods belong to the class instead of the objects, which means they are shared by all objects.


Static Attributes

If you create multiple objects of one class, the attributes normally have different values. But if you declare an attribute as static, all objects share the same value.

In this example, both objects share the same value of the static attribute x:

Example

public class Main {
  static int x = 5;

  public static void main(String[] args) {
    Main myObj1 = new Main();
    Main myObj2 = new Main();

    System.out.println(myObj1.x); // Outputs 5
    System.out.println(myObj2.x); // Also outputs 5
  }
}

Try it Yourself »

If one object changes the value of x, the change is seen by the other object too:

Example

public class Main {
  static int x = 0;

  public static void main(String[] args) {
    Main myObj1 = new Main();
    Main myObj2 = new Main();

    myObj1.x = 7; // Change x using myObj1
    System.out.println(myObj1.x); // Outputs 7
    System.out.println(myObj2.x); // Also outputs 7
  }
}

Try it Yourself »

Explanation: Because x belongs to the class, changing it through one object also changes it for all objects.


Normal Attributes

To see the difference, here is the same example but without static. Now each object has its own copy of x:

Example

public class Main {
  int x = 5;  // Normal attribute

  public static void main(String[] args) {
    Main myObj1 = new Main();
    Main myObj2 = new Main();

    myObj1.x = 9; // Change x in myObj1 only
    System.out.println(myObj1.x); // 9
    System.out.println(myObj2.x); // 5 (unchanged)
  }
}

Try it Yourself »

Explanation: Normal attributes belong to each object separately. Changing the value in one object does not affect the others.


Static Methods

Like static attributes, static methods also belong to the class. That means you can call them without creating an object first:

Example

public class Main {
  static void myStaticMethod() {
    System.out.println("Static methods can be called without objects");
  }

  public static void main(String[] args) {
    myStaticMethod(); // Call the static method
  }
}

Try it Yourself »

Tip: Static methods are often used for utility methods - small helper methods that can be reused, like doing math calculations or formatting text, without needing to create an object first.

Here we create our own utility method, square(), to calculate the square of a number:

Example

public class Main {
  static int square(int x) {
    return x * x;
  }

  public static void main(String[] args) {
    System.out.println(square(5)); // 25
  }
}

Try it Yourself »


Static Methods and Normal Attributes

A static method cannot use normal attributes directly, because normal attributes belong to objects, while static methods belong to the class:

Example

This will cause an error, because x is not static:

public class Main {
  int x = 10;

  static void myStaticMethod() {
    System.out.println(x); // Error: cannot use x here
    System.out.println("Static methods cannot use normal attributes directly");
  }

  public static void main(String[] args) {
    myStaticMethod();
  }
}

Try it Yourself »

Fix: To use a normal attribute inside a static method, create an object first:

Example

Main myObj = new Main();
System.out.println(myObj.x);

Try it Yourself »


Static Methods and this

The this keyword refers to the current object. But static methods belong to the class, not to objects. That means you cannot use this inside a static method.

Example

Using this in a static method will cause an error:

public class Main {
  int x = 10;

  static void myStaticMethod() {
    System.out.println(this.x); // Error
    System.out.println("Static methods do not have 'this'");
  }

  public static void main(String[] args) {
    myStaticMethod();
  }
}

Try it Yourself »


Normal Methods with Static Attributes

Normal methods can use static attributes, because static attributes belong to the class and are always available.

Example

Both objects share the same x value:

public class Main {
  static int x = 0;

  public void addOne() {
    x++;
  }

  public static void main(String[] args) {
    Main myObj1 = new Main();
    Main myObj2 = new Main();

    myObj1.addOne();
    myObj2.addOne();

    System.out.println("Value of x: " + Main.x); // Outputs 2
  }
}

Try it Yourself »


Why main() is Static

The main() method is static so that the program can run without creating an object first. Java needs to start running the program before any objects are made.


Summary

  • static attributes and methods belong to the class, not objects.
  • Static attributes are shared by all objects of the class.
  • Static methods can be called without creating objects.
  • Static methods cannot use normal attributes directly.
  • Normal methods can use static attributes.
  • Static methods cannot use this.
  • The main() method is static because the program must run before objects are created.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.