Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:13:40+00:00 2026-06-05T20:13:40+00:00

I have read that in Java interfaces can’t be instantiated ( in the documentation,

  • 0

I have read that in Java interfaces can’t be instantiated (in the documentation, Interfaces). Runnable, by definition is an interface which should be implemented by some class. But in the following piece of code from one of my Android applications I see that an empty constructor – Runnable()…(I am not sure if it is a constructor) has been used, and an instance has been created – getStatus.

final Runnable getStatus = new Runnable()
{
    public void run()
    {
        TextView uat = (TextView) findViewById(R.id.useAndThrow);
        uat.setText(MyThread.getStatus());
        return;
    }
};

What I have come to know:

  1. This is perhaps an example of anonymous class.
  2. The instance is not actually of interface.

But I am not able to connect the dots. Assuming that the above code was inside the myActivity class, what is being instantiated and how is this anonymous class?

A bit of detail would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:13:41+00:00Added an answer on June 5, 2026 at 8:13 pm

    It’s called an “anonymous class”. Some notes about it:

    1. You didn’t provide it with a name, so one is created for you. In order to see what its real name is, simply write it to the console or read the bytecode.

    2. The anonymous class, if created in the scope of the outer class, has a reference to its fields, just like any inner class. In general, it has the same scope of where it was created.

    3. It’s a very common thing to do if all you need is a simple implementation. The more code there is, the more you should consider putting it in a another file (for order, not because you can’t).

    4. Runnable is not the only interface that is common. On Android, you use OnClickListener, OnTouchListener, …

    5. Since the anonymous class has the same scope as to where it was declared, it means that it has a reference. Try to never keep an anonymous class for too long (for example, in a static reference) , since this could lead to memory leaks. On Android it’s very crucial, as this sample shows (talks about drawables, but an anonymous class can also have a reference to a context) .

    6. Some of the disadvantages of anonymous classes are that they don’t have a constructor, and that it cannot implement more than one interface. It has a very narrow usage, yet it’s very popular as it’s very easy to use (and read).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that in Java you don't have to explicitly bind the this
I have read up on it a bit and I understand that in java
I have read a document that they say: In java there two types of
I have read in a Java book that says: Because a String is immutable,
I am new to Java. I just read that class variables in Java have
I have read that you can do it, but would this really improve performance
Let's say I have the following Java interface that I may not modify: public
I have read http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html and noticed that all the examples have argument type declared
hi i am learning about Java interfaces. I read in Java tutorial that an
I have read that it is used for functions that implement system calls in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.