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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:33:02+00:00 2026-05-25T19:33:02+00:00

I was curious, I see this kind of thing a lot: Arrays.sort(array, new Comparator<Integer>()

  • 0

I was curious, I see this kind of thing a lot:

Arrays.sort(array, new Comparator<Integer>() {
    public int compare(Integer a, Integer b) {
        return Math.abs(a) < Math.abs(b);
    }
});

since the anonymous class created here has no instance variables, is the standard JDK compiler smart enough to only instantiate that anonymous class once and reuse it? Or is it advisable to instantiate that anonymous class in a static field and always pass the static Comparator object?

UPDATE: When I say “JDK compiler”, I mean the JIT portion too. The above is also just an example. I was really curious if I should, as a best practice, create static fields for the above instead of inlining anonymous class instantiations. In some cases the performance/resource usage issue will be negligible. But other cases might not be…

  • 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-25T19:33:02+00:00Added an answer on May 25, 2026 at 7:33 pm

    javac will definitely not do such thing; that would violate the language semantics. JVM could optimize it in theory, but it’s not that smart yet. A static one would be faster.

    Ironically, such analysis and optimization would be easy for javac, and it can be done today, except it’s forbidden to do so – the source says new, so javac must new.

    Rumor has it that the coming lambda expression in java 8 will make an effort on this issue, in

    Arrays.sort(array, (Integer a,Integer b) => Math.abs(a)<Math.abs(b) )
    

    javac is required to analyze that the lambda is stateless, and a lazily created single instance is enough, and that’s what it must compile the code into.

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

Sidebar

Related Questions

You might see this as a dumb question, but I'm curious about how I
I'm just curious to see what you guys think about this. I heard a
I am curious to see any alternative(s) to the regular if statements such as
I've always been curious to see if I can show, hide or change a
I've used XNA and Visual Studio 2005 integrated with Flash. I'm curious to see
A friend of mine downloaded some malware from Facebook, and I'm curious to see
Curious, what happens when you return keyword this from a struct in C#? For
I have something like this: public abstract class Menu { public Menu() { init();
I'm curious to see if anyone can shed some light on some strange text
I was curious, so I ran a couple of tests to see how .NET

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.