When to use static variables / methods and when to use instance variables / methods in Java?

I would like to ask a question when it would be useful to use static variables / methods or in other cases / instance variables in Java?

I know this depends on a specific case (for example, using usage classes as static methods), but can we declare something like a general strategy?

+7
java static instance
source share
5 answers

At the beginner level:

Use instance variables when: Each variable has a different value for a different object. For example: student name, roll number, etc.

use static variables when: The value of a variable is independent of objects (not unique to each object). For example: no students.

+12
source share

Static variable: when you need something that will be used through the application, and each instance must know the variable.

Instance variable: it will differ from object to object and object, and a static variable is a class property.

Static function: used to perform some service task. It can be called without declaring an object.

Instance function: you need an object to call this function.

static or instance depends on your goals.

+4
source share

static variables are often used for constants, which is common to all instances if a class. For example, many people do not like to โ€œhard codeโ€ constants in their code; they would like to make the variable public static or private static with a meaningful name and use it in their code, which should make the code more readable.

In short

Any method or variable that does not depend on the state of the class instance must be static .

+3
source share

Imagine that static variables are global class variables or, if you use the final keyword, as global global constants. Use static non-finite variables wisely - they are distributed among all instances of the class, and this can lead to some non-obvious errors. I would recommend avoiding the use of mutable static variables in general - there are practically no cases where such a need cannot be realized using dependency injection.

In addition, the use of global variables always makes multilevel device testing a further drawback.

+1
source share

As for the methods: each Foo.method (Bar1 b1, Bar2, b2) method, by definition, can always have alternative equivalent constructions:

Bar.altmethod (Foo f, Bar b2)

and

static staticmethod (Foo f, Bar b1, Bar b2)

And you could also wrap this last method as an intsance method in a service class, which itself is a singleton (so the static nature of the method is a little bit hidden by the class in which it is located).

The only convincing reason why your method is an instance method of a class of one of the method arguments (static version) is when you expect subclasses for this class to be available and that it can be useful for these subclasses to have a specialized method implementation.

Imagine

class GeographicalFigure {Object quadrature () {...}}

Perhaps it would be useful to leave the possibility of a later addition

the Circle class extends the geographic reference {Object quadrature () {throw new ThisIsNoGoodException (); }}

In addition, all of your options are essentially equivalent.

0
source share

All Articles