Java - Static Modifier

Java Conceptuel Diagram

About

Object - Static in Java.

Type

Method

The Java programming language supports static methods.

Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in

ClassName.methodName(args)

You can also refer to static methods with an object reference like

instanceName.methodName(args)

but this is discouraged because it does not make it clear that they are class methods.

A common use for static methods is to access static fields.

The Java platform lets you execute a class without creating an instance of that class as long as its static methods do not call any non-static methods or fields.

You can call any static method from anywhere. That's why they are best suited for:

java.lang.Math class is a perfect example where almost all methods are static.

Variable

The Java programming language supports static variables.

The static modifier in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

Member

You specify a class variable by using the static keyword in the member's declaration.

Without Static

A member that is not declared as static is implicitly an instance member.

(Non-Static Fields) Instance Variable. Technically speaking, objects store their individual states in “non-static fields”, that is, fields declared without the static keyword. Non-static fields are also known as instance (variables|member) because their values are unique to each instance of a class.

For example: the currentSpeed of one bicycle is independent from the currentSpeed of another.

Static

Class Variable (Static Fields) A class variable is any field declared with the static modifier.

Thread-Safe

Shared resources are not thread-safe. Hence Static method or variables are not thread safe. Constants using static and final keyword are read-only and therefore are thread safe.

If you want thread safety, you have to synchronize the static method. When a static synchronized method is invoked (a static method is associated with a class, not an object), the thread acquires the intrinsic lock for the Class object associated with the class. Thus access to class's static fields is controlled by a lock that's distinct from the lock for any instance of the class.





Discover More
Java Conceptuel Diagram
Java - (Field|Member Variable)

A field is a variables in a class and they are used to contain state information. They are also known as member variable. A member that is not declared as static is implicitly an instance member. ...
Java Conceptuel Diagram
Java - (Method|Functions)

A function that belong to an object is called a methods. A static method is then a sort of function. Methods: operate on an object's internal state and serve as the primary mechanism for object-to-object...
Java Conceptuel Diagram
Java - Class Variable (Static Fields)

A class variable is any field declared with the modifier static. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number...
Java Conceptuel Diagram
Java - Constant

The names of constant fields are in upper-case letters. By convention, the names of constant values are spelled in uppercase letters. If the name is composed of more than one word, the words are separated...
Java Conceptuel Diagram
Java - Dynamic Java code execution (at runtime)

How to create, compile and run Java code at runtime making it a dynamic, scripting language. The below section is showing parts of the whole script highlighting all the steps needed to create and run...
Java Conceptuel Diagram
Java - Functional Interface

in Java. A functional interface is is a interface that contains only one abstract method. The interface is so simple that java defines several standard (built-in) functional interfaces Functional...
Java Conceptuel Diagram
Java - Modifier

A modifiers affect runtime behaviour of class or method Access modifiers: public, protected, and private Modifier requiring override: abstract Modifier restricting to one instance: static Modifier...
Java Conceptuel Diagram
Java - Static utility

Static utility methods are particularly good candidates for generification. Writing generic methods is similar to writing generic types. The “algorithm” methods in Collections (such as binarySearch...



Share this page:
Follow us:
Task Runner