0

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?

2
  • 4
    Personally, I'm not sure nested classes are ever a good idea. Anonymous classes for event handlers - sure. Multiple classes in the same module: yes. Nested classes: I'd use inheritance or composition before I'd use nesting. IMHO... Commented Jun 17, 2012 at 3:53
  • 1
    @paulsm4 - Actually the decision to use nested classes is independent from whether to use inheritance or composition etc. Commented Jun 17, 2012 at 4:30

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.