Fundamentals 7 min read

Understanding Static Fields, Static Methods, and the final Keyword in Java

This article explains Java's static data members and methods, demonstrates how they are declared and accessed through a Human class example, shows how object constructors can modify static fields, and clarifies the purpose and usage of the final keyword in Java programming.

Java Captain
Java Captain
Java Captain
Understanding Static Fields, Static Methods, and the final Keyword in Java

Java classes are defined to create objects, where a class serves as a type classification and objects are functional entities; this basic object‑oriented concept is introduced before discussing inheritance.

Static data members (class fields) represent state shared by all instances of a class, such as a population field in a Human class that tracks the total number of humans.

class Human { public Human(int h) { this.height = h; } public int getHeight() { return this.height; } public void growHeight(int h) { this.height += h; } public void breath() { System.out.println("hu...hu..."); } private int height; private static int population; public static boolean is_mammal = true; }

Static fields are accessed via ClassName.field or through an object reference, but they must be declared with appropriate access modifiers; public static fields can be read or modified from outside, while private static fields are encapsulated within the class.

Static methods (class methods) are declared with the static keyword, cannot use instance variables or this , and can only operate on static data. An example adds a getPopulation() method that returns the static population field.

public static int getPopulation() { return Human.population; }

Class methods can be invoked either as Human.getPopulation() or via an object instance, as shown in a Test class that prints the population before and after creating a Human object.

public class Test { public static void main(String[] args) { System.out.println(Human.getPopulation()); Human aPerson = new Human(160); System.out.println(aPerson.getPopulation()); } }

Object methods can also modify static fields; the constructor of Human increments population each time a new instance is created, allowing the static field to reflect the total number of objects in real time.

The final keyword is explained: a final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be subclassed. Final references cannot point to a different object, though the object's internal state may still change.

When a variable is both static and final , it becomes a constant shared by all instances, suitable for values like π.

In summary, the article covers static fields, static methods, class‑level access, object‑level interaction with static data, and the usage of the final keyword in Java.

Javaprogramming fundamentalsOOPstatic methodsstaticfinalclass fields
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.