Last Updated:

Class methods and variables in Java

Class Members

The methods and variables we have in the Man class are instance methods and variables. Each instance will have them.

Classes themselves can also have variables and methods. They are collectively called class members, and you can declare them using the static keyword. The differences between class members and instance methods and variables are as follows:

  • Each instance of a class shares the same copy of the class variable.
  • You can call a class method with the class itself, without having an instance.
  • Instance methods can access class variables, but class methods cannot access instance variables.
  • Class methods can only access class variables.

When does it make sense to add class variables or class methods? The best practice is to do this rarely, so as not to abuse them. Some common examples of their use are:

  • Declare constants that can be used by any instance of the class.
  • Track the "counter" of class instances.
  • Use in a class with helper methods that do not require an instance to work with (for example, the Collections.sort() method).

Class variables

To create a class variable, use the static keyword when you declare it:

The JRE creates a copy of the variables of a class instance for each instance of that class. The JRE creates only one copy of each class variable, regardless of the number of instances immediately when you first encounter that class in your program. All instances will share (and possibly modify) this copy. This makes class variables a good choice for constants that all instances can use.

For example, we used integers to describe the "banknotes" in the Man wallet. This is perfectly correct, but perhaps it would be more correct to name the integer values in such a way that when we read the code, we can easily see what the numbers mean. Let's declare a few constants in the same place where we declared the instance variables for our class:

By convention, class constants are called large characters, and individual constant words are separated by underscores. We used the static keyword to declare them as class variables and added the final keyword to ensure that any instance could not modify them (that is, make them constants). Now we can change the main() method to add money to our Man object with these new constants:

When reading the code, it is obvious that we are adding money to the wallet.

Class Methods

As we have already seen, calling the instance method is as follows:

We called the method of a named variable that stores an instance of the class. The class method is called as follows:

We don't need an instance to call this method. To do this, the class itself is used. The main() method we use is a class method. Take a look at its signature. Note that it is declared as public static. We've seen this access specifier before. The static keyword indicates that this is a method of the class, which is why such methods are sometimes called static methods. We don't need an Man instance to call main().

We can create class methods for Man if we want, although there's really no reason to do so in this case. Although for demonstration purposes, let's add a trivial method of the class:

Comment out the existing lines of code in the main() method and add the following lines:

After running this code, you should see a corresponding message displayed twice on the console. The first call to doSomething() is the usual way to call a class method. You can also call it through an instance of the class, as you do in the third line of code.

But this, in reality, is not a very good practice. Eclipse will warn you about this by underlining this line with a yellow wavy line, and will suggest that you access this method "statically" through the class, not through an instance.