This question may seem dumb at first, but after having worked with different person, I see everyone seems to have their own, different knowledge about it, so here's my question.
So now I'm wondering what is the best way to do it, and why ("why" is more important for me):
I'm wondering about two methods to write Java code:
- Do you always pass Object or can you pass primitive data type ?
Do you call variables using
this.name
,name
orgetName()
inside your class instance ?public class MyClass { private String someStr; private int someNumber; private Integer someOtherNumber; // int, Integer ? which one to choose ? public MyClass(String someStr, int someNumber, int someOtherNumber) { // int someNumber ? Integer someNumber ? why ? this.someStr = someStr; // Here, it's clearly this.{name} = {name} because of the variable name conflict this.someNumber = someNumber; this.someOtherNumber = someOtherNumber; } public int someMethod(boolean first) { // Boolean ? boolean ? if (first) { return someNumber; } else { return this.someOtherNumber; // this.{name} ? just {name} or even this.get{name}() or get{name}() ? (supposing getters exists) } } }
I hope someone will provide me with a great explanation about which to use in order for me to write better code.