Can objects have the same name in a class? [closed]

0

I am starting in the world of programming, I have named two objects with the same name and there is no error.

I have a method that instantiates the class with this line:

clsDatos objdatos = new clsDatos();

... and another method that instantiates the class with the same line.

Is this good programming practice?

    
asked by Sebastian 16.03.2017 в 19:50
source

1 answer

1

Most likely, both objects were declared locally and not global, otherwise I would not have allowed it.

That is, you have two functions

public void funcion1() {
  int numero = 0;
}

public void funcion2() {
  int numero = 1;
}

Both functions have the same name, but because they are local they are destroyed when they leave the method, so they only have an impact within the same method. You could also do the same

 public void funcion1() {
  int numero = 0;
  funcion2();
}

public void funcion2() {
  int numero = 1;
}

And there would be no problem, the problem would come when you declare a global function and a local with the same name or two local functions with the same name or two global functions with the same name. The language will not know which variable with that name to point to, some languages like java if it allows you to create local variable and global variable pointing with this. to the global variable and if it is put without the 'this' it is pointing to the variable local.

private int numero;
public void funcion1() {
  int numero = 0;
  this.numero = numero //la variable global será igual a la local
}

But no language will allow you to create two local variables or two global variables with the same name.

Remember that most of the language is caseSensitive, it means that they are case-sensitive, not the same%% co% as%% co% or%%%

    
answered by 16.03.2017 / 19:59
source