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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:26:22+00:00 2026-06-03T06:26:22+00:00

Unmodifiable list in java can be created as: List<String> unModifiableStringList = Collections.unmodifiableList(myActualModifiableList); This is

  • 0

Unmodifiable list in java can be created as:

List<String> unModifiableStringList = Collections.unmodifiableList(myActualModifiableList);

This is fine, but what is the actual runtime type of the list returned by the above function? How can we access that class? Is it even possible?

UPDATE: Actually I need to know at compile time that an unmodifiable list is being modified, Since I have to deal with a lot of lists, some of which are modifiable and others are not. So it is very cumbersome to keep track?

  • 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-03T06:26:24+00:00Added an answer on June 3, 2026 at 6:26 am

    Actually I need to know at compile time that an unmodifiable list is being modified.

    That is not possible.

    Or at least, it is not possible without creating a completely different collections interface / class hierarchy. And that’s a bad idea because nothing designed to use regular collections would work with it.

    I suppose it would be possible to write a static code analyser that could detect this kind of thing … in some cases … but that’s not strictly “compile time”. Besides, I’m not aware of any existing static code analyser that does this “out of the box”.


    I wonder if there was a reason why they did it like this.

    Well none of the ways you might do this really work.

    Alternative #1:

     public interface UnmodifiableList<T> {
         public T get(int pos);
         ....
     }
    
     public interface List<T> extends UnmodifiableList<T> {
         public void add(T elem);
         ....
     }
    

    While static typing can prevent us from using an unmodifiable list where a modifiable one is required, the reverse is not true. Every List is also an UnmodifiableList … and that doesn’t really make sense.

    Alternative 2:

     public interface List <T> {
         public T get(int pos);
         public void add(T elem);
         ....
     }
    
     public interface UnmodifiableList<T> {
         // A marker interface
     }
    

    Now static typing can prevent us from using an modifiable list where an umodifiable one is required, but not the reverse. (That suits your requirement …) Furthermore, a class that implements UnmodifiableList still inherits the add operation, and there’s nothing to stop an application from trying to call it.

    In short, static type systems cannot adequately handle this kind of restriction.

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

Sidebar

Related Questions

In Java, one can use the Collections#unmodifiableList() method to create an unmodifiable list from
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
From ImmutableList javadocs: Unlike Collections.unmodifiableList(java.util.List), which is a view of a separate collection that
I would need a readonly / unmodifiable java.util.Stack . For List s there is
How would one do this? I have tried creating a new, empty list, then
Using Collections.unmodifiableMap(...) , I'm trying to return an unmodifiable view of a map. Let's
I'm looking for a way to test if some given List is an unmodifiable
I'd like to put unmodifiable wrappers around some of the Trove collections: I've checked
From Java 1.6 Collection Framework documentation : Collections that do not support any modification
Map<String,String> map=request.getParameterMap(); ^ is the unmodifiable map. Set s1= map.keySet(); Set s2= map2.keySet();/* another

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.