Head First Java Chapter 10 summary

Udesh Kavinda
6 min readNov 17, 2021

static is a non-access modifier in Java which is applicable for the following:

  1. blocks
  2. variables
  3. methods
  4. nested classes

To create a static member(block,variable,method,nested class), precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. For example, in below java program, we are accessing static method m1() without creating any object of Test class.

  • A static method should be called using the class name rather than an object reference variable: Math.random() vs myFoo.go().
  • A static method can be invoked without any instances of the methods’s class on the heap.
  • A static method is good for a utility method that does not (and never will) depend on a particular instance variable value.
  • A static method is not associated with a particular instance — only the class — so it cannot access any instance variable values of its class. It wouldn’t know which instance’s value to use.
  • A static method cannot access a non-static method since non-static methods are usually associated with instance variable state.
  • If you have a class with only static methods, and you do not want the class to be instantiated, you can mark the constructor private.
  • A static variable is a variable shared by all members of a given class. There is only one copy of a static variable in a class, rather than one copy per each individual instance for instance variables.
  • A static method can access a static variable.
  • To make a constant in Java, mark a variable as both static and final.
  • A final static variable must be assigned a value either at the time it is declared, or in a static initializer.

static{

DOG_CODE = 420;

}

  • The naming convention for constants is to make the name all uppercase.
  • A final variable value cannot be changed once it has been assigned.
  • Assigning a value to a final instance variable must be either at the time it is declared, or in the constructor.
  • A final method cannot be overridden.
  • A final class cannot be extended.

Static Imports

  • If you’re going to use a static member a few times, we think you should avoid static imports, to help keep the code more readable.
  • If you’re going to use a static member a lot, then it is probably OK to use the static imports.
  • Notice that you can use wildcards (*), in your static import declaration.
  • A big issue with static imports is that it’s not too hard to create naming conflicts.

What is the Final Keyword in Java?

Java final keyword is a non-access specifier that is used to restrict a class, variable, and method. If we initialize a variable with the final keyword, then we cannot modify its value.

If we declare a method as final, then it cannot be overridden by any subclasses. And, if we declare a class as final, we restrict the other classes to inherit or extend it.

In other words, the final classes can not be inherited by other classes.

This was a brief introduction to a final keyword in Java. Now we will discuss the implementation of the final keyword with a variable, method, and class in Java. We will discuss the following topics in detail:

  1. final variable
  2. final class
  3. final method

Final Variable in Java

Once we declare a variable with the final keyword, we can’t change its value again. If we attempt to change the value of the final variable, then we will get a compilation error.

Generally, we can consider a final variable as a constant, as the final variable acts like a constant whose values cannot be changed.

This rule was for the normal variables, what if we declare the reference variables as final? The restriction with the final reference variable is that we cannot change “what the object is referring to” but we can modify the object itself.

We can’t just declare a variable as final without initializing it. That is, we have to give it an initial value while declaring a final variable. If we declare the final variable without initialization, then it is a blank final variable.

But it is mandatory to initialize a final variable either during declaration or after declaration. If we leave it uninitialized, then we will get a compilation error.

final int number = 10; //final variable
final float value; //blank final variable
static final double rate = 2.5; //final and static variable

Final Method in Java

As earlier, we discussed the Final Keyword and How to declare the Final Variable. We can declare Java methods as Final Method by adding the Final keyword before the method name.

The Method with Final Keyword cannot be overridden in the subclasses. The purpose of the Final Method is to declare methods of how’s definition can not be changed by a child or subclass that extends it.

Final Class in Java

We can also declare a class with a final keyword in Java. When we declare a class as final, then we restrict other classes to inherit or extend it.

In short, Java final class can’t be extended by other classes in the inheritance. If another class attempts to extend the final class, then there will be a compilation error.

Wrapper classes in Java

The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.

Autoboxing

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short.

Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the primitive into objects.

Unboxing

The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives.

The format specifier

Everything after the percent sign up to and including the type indicator (like
“d” or “f”) are part of the formatting instructions. After the type indicator, the
formatter assumes the next set of characters are meant to be part of the output String, until or unless it hits another percent (%) sign. Can you have more than one formatted argument variable? Put that thought on hold for right now; we’ll come back to it in a few minutes. For now,
let’s look at the syntax for the format specifiers — the things that go after the
percent (%) sign and describe how the argument should be formatted.

--

--