0

I have to make a doubly linked list and I have to make an exception for when there is an overflow in the list, it is the first time I do it in Java, why does it appear as "dead code" in the program ?Isn't it done like this?

public void Insert(T element) {
        try {
        if (element == null) {
            throw new IllegalArgumentException();
        }
        Nodo newNodo = new Nodo(element);
        if (newNodo == null) {                   //Dead code ¿why?
            System.out.println("Overflow");
        }
        if (this.isEmpty()) {        
            this.head = newNodo;
            this.tail = newNodo;
        }
       ...
Andrea
  • 5
  • 2
  • 3
    because it is completely impossible for an instance to be null, the very line after you instantiate it. new Nodo(element); might throw an Exception, but it can't return null – Stultuske Mar 27 '21 at 17:47
  • [Dead code in if block?](https://coderanch.com/t/585549/java/Dead-code-block) – Ole V.V. Mar 28 '21 at 07:21

1 Answers1

3

Seeing as I don't know of a 'duplicate' question to flag, I'll put it in an answer.

A constructor looks (in some ways) like a method, but it isn't entirely the same. One of the (main) differences, is that it doesn't have a return statement, nor does it have a return type.

What it does, is create an instance of a class, and, if you assign that to a variable, as you did in your code, assigns it to said variable.

In a method, you could do something like this:

Nodo newNodo = getNodo(element);

while the getNodo method could do this:

public Nodo getNodo(T element) {
  if ( someValidationFailed(element)) {
    return null;
  }
  return new Nodo();
}

In this code, the null-check you wrote would make sense, because it is possible for this method to actually return null. A constructor, however, can't.

Nodo newNodo = new Nodo(element);
if (newNodo == null) {                   //Dead code ¿why?
  System.out.println("Overflow");
}

A constructor call can never result in null. It either results in the newNodo variable pointing to the new Nodo instance, or an Exception has been thrown.

If no Exception is thrown, the line after the Node newNodo = new Nodo(element); can not possibly evaluate to true, since newNodo can not be null.

If, however, an Exception is thrown, that line will never be reached. Either the Exception is propagated further up the chain, or it is caught in a catch block, but it 'll never reach the:

if ( newNodo == null ){

line.

EDIT: a small clarification, after Andy Turner's comment.

Constructors are used to create Objects based on the class blueprint. By using the new keyword, you creates space for the new Object in the memory, and its fields are initialized. Constructors

When you create an Object, using the new keyword, three steps are taken:

  1. Declaration Nodo newNodo = new Nodo(element);
  2. Instantiation Nodo newNodo = new Nodo(element);
  3. Initialization Nodo newNodo = new Nodo(element);

So,indeed, it's not the constructor that creates the instance, it's the "entire process of running the constructor using the new keyword" that does.

Steps

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • 1
    "What it does, is create an instance of a class" it doesn't (assuming by "it" you mean the constructor): it *initializes* the instance of the class. Creation of the instance and execution of the constructor are separate steps. – Andy Turner Mar 27 '21 at 19:37