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

209
Vistas
How to avoid repetitive parameter names (i.e. nearly the same name as an attribute of the object) when creating an object constructor?

I'm VERY new to OOP programming, so I apologize in advance if this is a stupid/irrelevant question

Below is a very simple object constructor in JavaScript

function Class(assign_to_a, assign_to_b){
    this.a=assign_to_a;
    this.b=assign_to_b;
}
object=new Class(2,3);

I do feel like I'm repeating myself a little. I've already named the attribute of the object inside the class, so why do I need to rewrite the same (slightly altered) name on a parameter whose sole purpose is to be assigned to an attribute that already has a descriptive name?

To better understand what I am trying to achieve, JavaScript already offers a neat solution for this:

function Class(){
    this.a=arguments[0];
    this.b=arguments[1];
}
object=new Class(2,3);

This way, I won't have to come up with/rewrite parameter names, when I already know that all the arguments will be assigned to an attribute of the object upon creation.

My problem is, I don't think too many other languages have an arguments object like JavaScript does (though I could be wrong about this), so I will have to find another workaround. Right now, I'm trying to create the same class in Java.

public class Class{
    int a, b;
  
    public Class(int assign_to_a, int assign_to_b){
        a = assign_to_a;
        b = assign_to_a;
    }

    public static void main(String[] args) {
        Class object = new Class(2, 3);
    }
}

Is there a way to work around coming up with repetitive names for the parameters of the object constructor, similar to what I did in JavaScript, or is this something I'll simply have to put up with?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Instead of creating multiple parameters, you could have the constructor take only one argument: an ArrayList. Then you can set the object's private and public properties by pulling them out of the contructor's ArrayList. In Java, this seems to be the closest match to JavaScript's "arguments object" (in terms of type flexibility).

I don't code in Java, but based on your example I assume that it might look something like this:

public class Class{
    int a, b;
  
    public Class(ArrayList ary){
        a = ary[0];
        b = ary[1];
    }

    public static void main(String[] args) {
        ArrayList ary = new ArrayList();
        ary.add(2);
        ary.add(3);
        Class object = new Class(ary);
    }
}

I have no idea if that will even compile, but hopefully it is good enough to relay the concept.

about 4 years ago · Juan Pablo Isaza Denunciar

0

As far as i know you can't avoid specify the arguments in a method signature in Java.

But I think that it's a great feature of Java, because it's check the parameters type at compilation time and avoid all the problems that comes when you pass the wrong arguments to a function.

If you want avoid repetitive and boilerplate code there are lot of libraries to help, like "Project Lombok".

about 4 years ago · Juan Pablo Isaza 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