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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:34:04+00:00 2026-06-15T13:34:04+00:00

In a different post , I was told that it is wrong to define

  • 0

In a different post, I was told that it is wrong to define an anonymous inner class “after” (below) a function which uses it. However, something like the below compiles and runs fine:

public class CompTest {

    public static void main(String[] args) {
        TreeSet<Integer> ts = new TreeSet<Integer>(intComp);
        ts.add(1);
        ts.add(2);
        ts.add(3);
        System.out.println(ts.toString());
    }

    private static Comparator<Integer> intComp = new Comparator<Integer>() {
        @Override
        public int compare(Integer i1, Integer i2) {
            return i2.compareTo(i1);
        }
    };

}

What is the official word on this? My guess is that since intComp is static, then it is instantiated once when the class CompTest is “loaded” (not sure exactly how that load takes place since there is only a main method and no CompTest objects are constructed), and therefore when main() needs intComp, it is available regardless of where in the source file it was actually defined.

And even though it does work (for the above reason or even a different one)… is it bad practice?

  • 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-15T13:34:04+00:00Added an answer on June 15, 2026 at 1:34 pm

    You are refering to this comment I believe:

    “The comparator definition should be before the add code – Bryan Glazer”

    There is no technical reason to put the intComp declaration before (or after) the main declaration. It works fine either way.

    The only possible reason for putting the comparator earlier is stylistic. That is, the intComp declaration is a static variable, and it is conventional to declare variables at the start of a class.

    I’m inclined agree with the style point, but it is a minor issue. Also, if it was me, I’d declare the comparator as final, give it a more meaningful name, and use the “constant” naming style; e.g. INT_COMP (but more meaningful).


    Re Nambari’s comment:

    “I think it is bad practice, anonymous class are for limited scope not for global scope.”

    This is nonsense. It is quite common for anonymous classes to be used in this way. Sure, anonymous classes can also be used in nested scopes … and that is one of their advantages … but that in no way means that this pattern is wrong.

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

Sidebar

Related Questions

In a different post I got a reply that tells about Repetitive SQL. Could
I have noticed that cURL in PHP returns different data when told to output
I found this code snippet on a different post that I don't quite understand
Greetings all, I've been told by co-workers that the GET method is different in
I'm developing a wordpress plugin. I'm using two different custom post types, players and
So I have 2 different group of tags in one post, let's call them
I would like to alter two different tables from an HTML post request to
I have seen a similar related post but my situation is a little different
I had a different problem first, so I made this post: Java JPanel mouse
The higher-ups in my company were told by good friends that flat files are

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.