Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Somebody Explain this java code

Tags:

java

android

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);
            }
        }
    };   
like image 788
dubbeat Avatar asked May 25 '10 13:05

dubbeat


5 Answers

  • 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.

like image 97
Richard Walton Avatar answered Oct 17 '22 02:10

Richard Walton


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?)

like image 32
Charlie Collins Avatar answered Oct 17 '22 02:10

Charlie Collins


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

like image 3
Rob Di Marco Avatar answered Oct 17 '22 02:10

Rob Di Marco


This code creates an instance of class Handler, and at the same time overrides the "handleMessage" method of Handler.

like image 2
Richie Avatar answered Oct 17 '22 04:10

Richie


This is an anonymous class which (a) implements the Handler interface or (b) extends the Handler class.

like image 2
Andreas Dolk Avatar answered Oct 17 '22 04:10

Andreas Dolk