"should be accessed in a static way"

Frank Harris :

I have a class with a probably unnecessarily cumbersome name, that contains a lot of static methods I use elsewhere.

Rather than fill my code with a lot of

VeryUnnecessarilyLongCumbersomeName.doThingFoo();
VeryUnnecessarilyLongCumbersomeName.doThingBar();
VeryUnnecessarilyLongCumbersomeName.doThingEgg();
VeryUnnecessarilyLongCumbersomeName.doThingSpam();

I would rather have

VeryUnnecessarilyLongCumbersomeName thing = new VeryUnnecessarilyLongCumbersomeName();
thing.doThingFoo();
thing.doThingBar();
thing.doThingEgg();
thing.doThingSpam();

However, this gets the warning

"the static method doThingFoo() should be accessed in a static way."

I know there are multiple solutions here. Use better class names. Make it not static. Ignore it because it's just a warning.

But I don't actually think it should be a warning. What harm does doing it this way cause? Is there a more elegant/correct way to make my code less clunky that isn't one of the above solutions?

NOTE: I suspect this might warrant the coding-style tag and therefore be considered off-topic and get rejected. I was thinking there's room here for a question like this, however, so I leave it up to y'all.

Nexevis :

Although it is not technically harmful because it technically works, the problem with this is it is misleading, and any values that the instance thing contains, do not actually matter at all for the results of the methods.

Typical Java Convention:

When accessing a method through an instance, one would expect the result to be dependent on the values of the instance.

When accessing a method through a Class name, one would expect the result to be independent of the values of any instance.

Your way:

You are accessing a method through an instance and expecting it to be independent of any instance.

So why use an instance for an instance independent method? That is why it is misleading. I would suggest attempting to shorten the class name rather than accessing static methods through an instance.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=6024&siteId=1