Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put inner classes? [closed]

Some might like to argue that this is a candidate for the least important issue of all times. Yet code style is a very important topic for me, and I want to ensure that I write code in a readable way - for me and the majority of developers.

That's why I'm wondering where you guys are declaring your inner classes.

I'm following the following method ordering scheme, because it is quite common:

public void foo() {     usedByFoo(); }  private void usedByFoo() { }  public void bar() { } 

I order them from top to bottom, every method as close to where it is used.

Now I could do the same with inner classes, like this:

class Outer {     private Inner inner;      private class Inner {};      public Outer() {     }      ... } 

I think this is the most consistent style to follow for me, but I've also often seen people declare all inner classes either at the top or at the bottom of the file.

Which style should I follow, given my way of ordering methods? What is the most common way to do this?

like image 701
Jason Noack Avatar asked Jan 18 '11 23:01

Jason Noack


People also ask

Where do you put inner classes?

I would declare inner-classes in the bottom of the file - usually you're not interested in their implementations and just want to get to your main class' methods, so they shouldn't get in the way.

Can inner classes be final?

Local Inner classes are not a member of any enclosing classes. They belong to the block they are defined within, due to which local inner classes cannot have any access modifiers associated with them. However, they can be marked as final or abstract.

Can inner class be protected?

protected Inner Class There is one more particular case — a protected inner class. As we can see, this is a static inner class, and so can be constructed from outside of an instance of FirstClass. However, as it is protected, we can only instantiate it from code in the same package as FirstClass.


1 Answers

I would declare inner-classes in the bottom of the file - usually you're not interested in their implementations and just want to get to your main class' methods, so they shouldn't get in the way.

like image 63
Amir Rachum Avatar answered Sep 21 '22 23:09

Amir Rachum