fadden wrote:
>
> Greg Krimer  wrote: 
> > I have been finding it convenient to extend Handler in many of my 
> > activities to handle messages specific to the activity. The handler 
> > sublass is an inner class of the activity and needs to access its 
> > state. I am wondering if there is any performance difference between 
> > making the handler subclass static and passing in the activity 
> > explicitly in its constructor or making the subclass an "instance 
> > class" and letting the vm worry about my accessing members of the 
> > containing activity. 
>
> There's no real difference between explicitly and implicitly passing 
> the outer-class reference around.  Your example above is essentially 
> doing what javac does automatically. 
>
> Static inner classes have some useful properties, e.g. you know 
>

There is no such thing as a "static inner class" in Java.
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3>

A nested class is either static or inner, but cannot be both at once.
 

> they're not modifying state in the parent, and you can use 
> Class.newInstance() with them.  There's no performance magic though. 
>

Being able to use 'Class.newInstance()' isn't exactly a selling point.
 

> If you're really curious, write it both ways in trivial source files, 
> compile them, then view them with "javap -private -verbose <Class>" or 
> "dx --dump <Class.class>".  They should look about the same. 
>

Asking about performance differences at this level is the height of 
silliness.

Have you profiled your code to find if there's even the slightest hint 
of a whiff of a suggestion of an indication of a possibility of a bottleneck
in your use of nested classes?

I thought not.

Use what makes the most sense *in the code* to express the logic.
In your case this sounds like using inner classes instead of 
static nested classes.

The rule of thumb is that inner classes are for when the inner 
instance needs access to the outer instance's state, which you 
say you do. Ergo, use inner classes.

The rule of thumb is that static nested classes are for when 
the nested-class instance doesn't need access to the enclosing 
type's instance state, but its semantics are highly local to 
the enclosing type's needs.
 
Don't waste time on optimizations that don't optimize. Focus 
on writing code that clearly and directly expresses what it should
express.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to