Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
Variable has not have been initialized in Java

The following code fails to compile as the variable 'rt' has not been initialized.

I'm wanting the code to either initialize 'rt' in the switch-Statement, or not use 'rt' anymore when the default case is ran.

Here is the code I've written:

enum RequestType {leader, candidate};

private void processRequest(String input) {


        boolean inputOK = true;
        RequestType rt;

        String[] split = input.split(":");

        if (!input.contains(":")) {
             inputOK = false;
        }
        else {


            if (split.length != 3) {
                inputOK = false;
            }

            else {
                int port = Integer.parseInt(split[2]);

                if (!(port > 0 && port < 65537)) {
                    inputOK = false;

                }

                else {

                    switch (split[0]) {
                        case "Leader":
                            rt = RequestType.leader;
                            break;
                        case "Candidate":
                            rt = RequestType.candidate;
                            break;
                        default:
                            inputOK = false;
                    }

                }
            }
        }

        if (!inputOK) {
            return;
        }

        String address = split[1].concat(split[2]);

        if (rt == RequestType.leader) {

            keepingLeader(address);
        }

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You don't initialize rt in the default block, or many of the ifs.

The compiler doesn't look at whether the read of rt is reachable with respect to the value of inputOK, it just looks at whether it is reachable at all.

The easiest thing to do is just to return immediately where you currently have inputOK = false;.

Alternatively, you can assign a default value to rt, for example null, when you declare it. The problem with that is it breaks the checking that the compiler is doing now - that you actually have assigned it in all circumstances, so you don't accidentally use the default value.

Instead, you can use the nullity of rt instead of having a separate boolean value:

  • Where you currently assign inputOK = false, assign rt = null instead;
  • Where you then check !inputOK and return if true, check rt == null instead.
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda