-1

I wanted to know what's the difference between using a setter and getter to get & initialize an instance variable vs naming your own method to set and get an instance variable?

Does it change anything when calling setCar() to set a variable instead of mesetCar() or initializeCar()?

PS: This is not why use getters and setters instead of public fields its why name the methods setters and getters instead of a random method name

  • 3
    One conforms to the Java Bean standards first laid down in 1995; the other does not. Any technology that depends on those standards will break if you use the random names. Worse, other people will have a harder time reading and understanding your code. Deviate from the standards only when you have a very good reason. – duffymo Oct 29 '17 at 22:42
  • 3
    *"does it change anything"* - Well, it's likely to change people's mood, as most Java developers are use to using things like `setXxx`/`getXxx`. They are, for the most part, also self documenting. I'd also question any method called `initialize`, as the constructor should have done that – MadProgrammer Oct 29 '17 at 22:43

1 Answers1

0

No implications. Only a convention.

public void setName(String nm){
    Name = nm;
}

public void makeName(String nm){
    Name = nm;
}

These would perform the same function.

Herwood99
  • 41
  • 7