What is the Java convention for using get methods in other methods of the same class?

After I wrote the get method in the java class, is it better to use the get method in the same class or the variable itself?

For instance:

if(a.getWidth()>this.getWidth()) 

or

 if(a.getWidth()>this.width) 

Also I am confused if I have to use this.anything so much. It seemed easier to read when comparing objects of the same type with each other.

+4
source share
6 answers

1) From within the class, it might seem that there is no difference between using a field and a getter, but what if the getter is overridden by a subclass?

 class A { String name; String address; String getName() { return name; } String getAddress() { return address; } String getDescription() { return name + " " + address; } } class B extends A { String country; @Override String getAddress() { return super.getAddress() + ", " + country; } } 

B.getDescription () is expected to return an extended address, but will not. This would be if A.getDescription () was implemented as

  return getName() + " " + getAddress(); 

2) I personally do not use this for reading, because the IDE marks this different color

+2
source

is it better to use the get method in the same class or variable itself?

IMHO use a variable. Access methods are intended primarily for using other objects.

Also I am confused if I have to use this.anything so much. It seemed easier to read when comparing objects of one type with each other.

You do not always need to explicitly use the this .. link. It is mainly used for reading, as you said.

+3
source

I think using getter methods is better for convenience. Consider a template of a null object , the way to achieve which is as follows:

 public String getName(){ if (this.name == null){ this.name = ""; } return this.name; } 

This should save you from checking a large number of zeros before working with a variable.

 public boolean isCorrect(){ if(this.name != null && this.name.isEmpty()){ //The null check up is boilerplate code return false; }else{ return true; } } 

I would rather write this:

 public boolean isCorrect(){ if(this.getName().isEmpty()){ //The null check up is boilerplate code return false; }else{ return true; } } 

Of course, it depends if you accept this template.

Also consider that you have

 double width; double height; public double getWidth(){ return this.width; } 

but at some point you decided to change it for the class, but you still have methods to prevent your program from breaking.

 Dimension dimension; public double getWidth(){ return this.getDimension().getWidth(); } // etc... 

Finally (as MadProgrammer commented), when you use inheritance, methods can be overridden to better represent the intended object.

+3
source

Using this is optional if you do not have a case (for example, in the constructor) where the parameter has the same name as the field.

For accessing properties, it may ultimately be useful to use public getters, because you may want to add some form of processing to the property, and if you use getters everywhere, you only need to make this change once.

0
source

If your get method returns data with some formatting, you should use the get method, otherwise the variable itself will be useful to use.

this is only required if the parameters of your method match your member variables, otherwise it is optional.

For instance:

 private String str; public void setString(String str){ this.str = str; // here this.str is necessary because this represents the memeber variable } public String getString(){ return this.str; // here this is optional and you can simply omit it } 
0
source

You can use either the accessory or the variable itself, one of those personal preferences.

Some people like to use this variable because you do not have the overhead of calling a function. But, if you have any restrictions on the values ​​that your variables may be, sometimes it’s just easy to use your accessors and mutators; especially if you are going to subclass. But this is one of those things that can go anyway.

As I like to use the this , I always use it, for example, for variables. I believe this makes it easier to read the code, as you can visually determine where you use instance variables and where you use local variables. Again, this is a personal preference.

The main thing is that your code is clean and readable. Also, make sure that you comply with all the coding standards used by your organization, and make sure that they are consistent with each other.

0
source

All Articles