I’m working on a simple android live wallpaper, I’m following chapter 12 from Hello, Android as my guide.
The bare-bones of a wallpaper service looks like this:
public class MyWallpaper extends WallpaperService {
private class MyEngine extends Engine {
//...
}
//...
}
According to the book MyEngine must be an inner class of MyWallpaper. I have no reason to dispute this, but the book offers no explanation as to why this must be so. I prefer not to use inner classes purely for stylistic/aesthetic reasons.
I was wondering if MyEngine actually has to be a private inner class and, if so, why?
You’re supposed to do it this way because
class Engineis nested within theabstract class WallpaperService. If you try to make it not nested, your IDE/compiler will tell you something like this:Which, loosely translated, means “you could do it that way, but it’s going to end up uglier than if you just use the nested class.”