Java: Static

This entry is part 32 of 54 in the series Learn Java

The static keyword is used to create fields and methods that can be accessed without having to make an instance of the class.

Static (class) members only exist in one copy, which belongs to the class itself, whereas instance (non-static) members are created as new copies for each new object.

This means that static methods cannot use instance members since these methods are not part of an instance.

On the other hand, instance methods can use both static and instance members.

package javaapplication25;

class MyCircle {

    float r = 5;            // instance field
    static float pi = 3.14F; // static/class field

    // Instance method
    float getArea() {
        return newArea(r);

    // Static/class method
    static float newArea(float a) {
        return pi * a * a;
    public static void main(String[] args) {
        MyCircle c = new MyCircle();
        float x = c.getArea();

To access the getArea method, we create an instance c for MyCircle class and access the method using a dot as in c.getArea().


Series Navigation<< Java: Garbage collector
Java: Accessing static members >>

Leave a comment

Leave a Reply