简体   繁体   中英

Can Somebody Explain this java code

I'm just starting out on android and my java is verry rusty. I can't remember ever seeing a function nested in another function like this before. Could somebody explain to me exactly what final does and explain why you would nest a function in another like this?

private final Handler handler = new Handler() {
        @Override
        public void handleMessage(final Message msg) {
            Log.v(Constants.LOGTAG, " " + ReviewList.CLASSTAG + " worker thread done, setup ReviewAdapter");
            progressDialog.dismiss();
            if ((reviews == null) || (reviews.size() == 0)) {
                empty.setText("No Data");
            } else {
                reviewAdapter = new ReviewAdapter(ReviewList.this, reviews);
                setListAdapter(reviewAdapter);
            }
        }
    };   
  • This is an Anonymous Class . What is actually happening is that a subclass of Handler is being created with an overridden handleMessage function.

    One of the most elegant things about anonymous classes is that they allow you to define a one-shot class exactly where it is needed. In addition, anonymous classes have a succinct syntax that reduces clutter in your code.

  • You also asked "Could somebody explain to me exactly what final does". A nice explanation can be found here .

    In the case of your example the final keyword stops anybody from being able to assign a new instance / null the instance of the variable "handler" meaning I cannot write the line handler = null; or handler = new Handler() { ... } after your example code snippet.

It seems that I wrote that Java code ;). That's from one of my examples in the Manning book Unlocking Android - http://code.google.com/p/unlocking-android/ . While the goal of the book was not to cover Java in detail, and rather to focus on Android, I apologize if it caused confusion.

The answers here are spot on, that's intentionally an anon inner class because it not used anywhere else outside of that context.

One note that I would add though, if you are just starting out with Android, I would go with a newer book. Mark Murphy's CommonsWare books are quite good ( http://commonsware.com/Android/ ) and he keeps them very up to date (or maybe look at Unlocking Android Second edition, I am not involved with that, and haven't read it so can't recommend or not, but Manning is working on it and it is available as early access at their site).

It might seem strange for an author to be recommending a book other than his own, but the truth is there are some better ways to do that stuff now on the newer APIs (that was written at 1.0 level, and tested on 1.5 after the fact). Don't get me wrong, that book still has a lot of value (that I am proud of) in terms of overall concepts (life cycle, activities, intents, etc.), but there are better/easier/newer ways to get some specific tasks done.

For example, have a look at AsyncTask ( http://android-developers.blogspot.com/2009/05/painless-threading.html ) and it will save you a lot of Handler/Message pain.

(I would have commented here, but I can't figure out how to comment rather than "answer," it may be related to not having enough of a rep?)

This is known as an anonymous class. A detailed explanation available here.... (link)

此代码创建类Handler的实例,同时覆盖Handler的“handleMessage”方法。

这是一个匿名类,它(a)实现Handler接口或(b)扩展Handler类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM