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

  • SEARCH
  • Home
  • 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 8115779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:34:39+00:00 2026-06-06T03:34:39+00:00

I am new to Java. I have problem to understand the relation between anonymous

  • 0

I am new to Java. I have problem to understand the relation between anonymous class instance and interface. Refer to the example in this website:

// anonymous instance as a variable
Runnable r = new Runnable()
{
    @Override
    public void run()
    {
        //codes
    }
};

Thread t1 = new Thread(r, "anonymous 1");

// anonymous instance as a parameter
Thread t2 = new Thread (new Runnable()
{
    @Override
    public void run()
    {
        //codes
    }
}, "anonymous 2");

Based on the answers in this SO question (http://stackoverflow.com/questions/11069056/why-java-interface-can-be-instantiated-in-these-codes), I have a basic understanding of anonymous class.

However, I don’t understand what is this:

Runnable r = new Runnable(){...};

On the right hand side, we created an instance of an anonymous class, so it is an object of a class. On the left hand side, it is an Interface variable.

Why an instance of anonymous class can be assigned to an Interface variable?

Refer to this website:

Thread(Runnable target, String name)

It seems that Thread is expecting the 1st argument to be an Interface variable.

  • 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-06T03:34:41+00:00Added an answer on June 6, 2026 at 3:34 am

    This code…

    Runnable r = new Runnable(){...};
    

    The class Runnable is an interface, so you are correct that you can’t create an instance of it directly. However, note the code in the {...} section – this is implementing the methods of the interface in a localised way.

    Basically what is happening is you are creating an instance of the interface, which is only viewable and usable by the method where r is defined. As you have implemented all the methods in the {...} section, the class is valid and you can use it as you do any other Runnable object.

    This code…

    Runnable r = new Runnable()
    {
        @Override
        public void run()
        {
            //codes
        }
    };
    

    Is effectively the same as doing the following…

    public class MyRunnable implements Runnable {
        public void run()
        {
            //codes
        }
    };
    
    // This goes in your class where you want to create the Runnable object
    Runnable r = new MyRunnable();
    

    Both pieces of code will create a variable r that is a Runnable instance. The first solution is a shorthand for creating the instance, and is useful for something that won’t need to be reused anywhere else. The second solution creates a separate class for implementing the interface, and it can be reused outside of the local scope.

    We are able to store the variables with an interface type, provided that the object assigned to the variable implements the interface. So, in the code…

    Runnable r = new Runnable(){...};
    

    We are saying that r implements the Runnable interface. The bit in the {...} is where the interface is implemented, as explained earlier, so it all works out fine.

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

Sidebar

Related Questions

I'm new here and kinda new to java. I've encountered a problem. I have
I am new to Java. I just read that class variables in Java have
I have a problem with a really slow connection between my Java code and
I'm relatively new in the Java world and I have a problem which I
I have a Java class that looks like this: public class My_ABC { int
I´m having a stupid problem with java.util.Date . I have this line of code,
I have a weird problem with a Java Gregorian Calendar: SimpleDateFormat sdf = new
I am new in Java and have got as a task to find out,
Completely new to java and I have been playing around with regex in a
I'm new to Java and I have a beginner question: NumberFormat is an abstract

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.