Java: Accessing static members

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

To access a static member from outside the class, the class name is used followed by the dot operator.

This operator is the same as the one used to access instance members, but to reach them an object reference is required.

Trying to access a static member by using an object reference (instead of the class name) will give a warning since this makes it more difficult to see that a static member is being used.

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) {
        float f = MyCircle.pi;
        MyCircle c = new MyCircle();    
        float x = c.getArea();       


Series Navigation<< Java: Static
Java: Advantage of static members >>

Leave a comment

Leave a Reply