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 1113699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:53:47+00:00 2026-05-17T02:53:47+00:00

I was reading Object Oriented Javascript and found the concept of closures. I didn’t

  • 0

I was reading Object Oriented Javascript and found the concept of closures. I didn’t quite understand why and when it is used. Do other languages like Java also have closures? I basically want to understand how knowing the concept of closures can help me improve my coding.

  • 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-05-17T02:53:48+00:00Added an answer on May 17, 2026 at 2:53 am

    A closure is a first class function with bound variables.

    Roughly that means that:

    • You can pass the closure as a parameter to other functions
    • The closure stores the value of some variables from the lexical scope that existed at the time that is was created

    Java initially didn’t have syntactic support for closures (these were introduced in Java 8), although it was fairly common practice to simulate them using anonymous inner classes. Here’s an example:

    import java.util.Arrays;
    import java.util.Comparator;
    
    public class StupidComparator { 
        public static void main(String[] args) {
            // this is a value used (bound) by the inner class
            // note that it needs to be "final"
            final int numberToCompareTo=10;
    
            // this is an inner class that acts like a closure and uses one bound value
            Comparator<Integer> comp=new Comparator<Integer>() {
                public int compare(Integer a, Integer b) {
                    int result=0;
                    if (a<numberToCompareTo) result=result-1;
                    if (b<numberToCompareTo) result=result+1;
                    return result;
                }
            };
    
            Integer[] array=new Integer[] {1,10, 5 , 15, 6 , 20, 21, 3, 7};
    
            // this is a function call that takes the inner class "closure" as a parameter
            Arrays.sort(array,comp);
    
            for (int i:array) System.out.println(i);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading Object-Oriented Javascript by Stoyan Stefanov, and at one point he
Currently, I'm reading 'Object-Oriented JavaScript'. In addition, I've encountered a hiccup while carrying out
I'm reading some slides of a class on object oriented programming languages and stepped
I keep reading that object-oriented programming can basically be done in any programming language,
I was reading Design Patterns: Elements of Reusable Object-Oriented Software , (specifically the chapter
Reading a book I have found the following statement: (Object) Behaviors answer either of
a) Can Object.GetType also be used for late binding ( Book I’m reading says
I'm currently reading Head First's Object Oriented Analysis and Design. The book states that
I was reading up a bit on Nicole Sullivan 's theory on Object-Oriented CSS
I'm refactoring some code in project whilst reading Object-Oriented Reengineering Patterns specifically the section

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.