Is a Java object with hundreds of methods expensive?

I have a class like below, with hundreds of methods:

public class APIMethods {   public ToView toView;  public APIMethods(ToView toView) {     this.toView = toView; }   public static final int SUCCESS = 1; public static final int ERROR = 0;   public void registerAnonymous(String deviceId, String installRef, final int requestCode) {     APIInterface apiService =             RetrofitClientInstance.getRetrofitInstance().create(APIInterface.class);     JsonObject obj = new JsonObject();     obj.addProperty("androidId", deviceId);     obj.addProperty("projectId", 0);     obj.addProperty("ChannelName", installRef);     Call<Response<BasicUser>> call = apiService.registerAnonymous("application/json", Utils.getFlavorId(), obj);     call.enqueue(new Callback<Response<BasicUser>>() {         @Override         public void onResponse(Call<Response<BasicUser>> call, Response<Response<BasicUser>> response) {             Response<BasicUser> mResponse;             try {                 mResponse = response.body();                 if (mResponse.getErrorCode() == 0)                     toView.updateView(requestCode, SUCCESS, mResponse);                 else                     toView.updateView(requestCode, ERROR, mResponse);             } catch (Exception e) {                 mResponse = new Response<>();                 mResponse.setErrorCode(-1);                 toView.updateView(requestCode, ERROR, mResponse);                 e.printStackTrace();             }         }          @Override         public void onFailure(Call<PetMarkResponse<BasicUser>> call, Throwable t) {             Response<BasicUser> numberValidationResponse = new Response<BasicUser>();             numberValidationResponse.setErrorCode(-1);             toView.updateView(requestCode, ERROR, numberValidationResponse);         }     }); }  ///And dozens of such method } 

So in my other classes everywhere in my application, I simply instantiate the class and call the method that I want:

APIMethods api = new APIMethods(this); api.registerAnonymous(Utils.getAndroidId(this), BuildConfig.FLAVOR, STATE_REGISTER_ANONYMOUS); 

My question is how expensive this object (api) is? Note that in each class, a few methods of the object are called.

Add Comment
4 Answer(s)

The object is not expensive at all.

An object contains a pointer to the object’s class, and the methods are stored with the class. Essentially, the methods are all shared. An object of a class with no methods and an object of a class with 10000 methods are the same size (assuming everything else is equal).

The situation would be different if you had 100 fields instead of 100 methods.

You may want to think about if having hundreds of methods in a single class is a good idea. Is the code easy to understand and maintain? Is this an example of the "God object" anti pattern? https://en.m.wikipedia.org/wiki/God_object

Answered on July 16, 2020.
Add Comment

This seems like a classic example of the XY problem. Your actual problem is how to make the code readable, but you’re actually asking about whether a class with hundreds of methods is expensive.

It being expensive is the least of your concerns – you should be more worried about maintenance. There’s no reason at all that any class should ever be that large, especially if you have a lot of independent methods and each class is only calling a few of them. This will make the class very hard to understand – having them all in one place will not improve the situation.

Some of the comments have already pointed this out, but you should, at a minimum, break this up topically.

Even better, refactor this to the Strategy pattern and use a Factory to pick which one to use. That will meet your goal of ease of use while avoiding the problem of having hundreds of unrelated methods in one place.

Add Comment

Try to define a Cohesive class, untill and unless the methods are written relevant to the class and it defines its purpose.

Below link describe the importance of methods for a class:

https://www.decodejava.com/coupling-cohesion-java.htm

Answered on July 16, 2020.
Add Comment

Try to define a Cohesive class, until and unless the methods are written relevant to the class and it defines its purpose.

Below link describe the importance of methods for a class:

https://www.programiz.net/java/cohesion-in-java/

Answered on November 7, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.