praks5432
praks5432

Reputation: 7792

Java- correct use of nested class?

I have a class called Flamethrower which naturally has its own ammunition that is distinct from other weapons. Should this ammunition be a nested class within flamethrower as only flamethrower will ever use this class?

Upvotes: 0

Views: 107

Answers (2)

Antimony
Antimony

Reputation: 39451

You probably shouldn't have a class at all. Generally, you want stuff like this to be scriptable, for ease of development and modding. You should replace it with a generic ammo class that reads values from a data file in order to customize behavior unless you have a really good reason not to. In general, a common mistake among Java beginners is to make classes for everything whether they need it or not.

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

I'm going to delete my comment and make this an answer:

Should this ammunition be a nested class within flamethrower

I don't think so, the reason being that the ammunition will interact with the target as well, and so its effects are felt beyond that of its host weapon. I usually reserve inner classes for "helper" classes that are used only inside of the outer class.

Upvotes: 3

Related Questions