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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:16:09+00:00 2026-06-17T08:16:09+00:00

I have a problem with the implementation of an update in the obersver pattern

  • 0

I have a problem with the implementation of an update in the obersver pattern in JAVA.
I send an ArrayList coming from my Queueing system to my Observer/s. The problem now is in the Observer where i receive the ArrayList as an Object. The next step would be a cast into an ArrayList but due to the runtime of Java I have no parameter information, which results in a unsafe cast. I mean i could notify my Observer with a whole generic class but i dont know if I am mistaken or it is “shooting with boulders on small birds”. In result there is no error in the end cause I always notify this observer with ONE typ but the question is should I take the warning or is there a workaround.

            ArrayList<Message> messageList = sqsUtil.getMessagefromAnotherEntrypoint("myQueueName");
        if (messageList != null) {
            // notify Observers
            notifyObservers(messageList);

        }

And then the the Update where the unsafe cast happens

    @Override
public void update(Observable o, Object arg) {
    ArrayList<Message> aMessageList = (ArrayList<Message>)arg;

}

Nevertheless thanks for Help

  • 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-17T08:16:10+00:00Added an answer on June 17, 2026 at 8:16 am

    JLS §5.1.9 talks about unchecked casts and when/why they happen. Long story short:

    • You don’t check that arg is actually an ArrayList (i.e. arg instanceof ArrayList<?>).
    • There’s no possible way to check if arg is an ArrayList that contains Message objects (at least not without using instanceof on each of the elements in the ArrayList). In other words, arg instanceof ArrayList<Message> doesn’t compile.

    If you want to get rid of the warning (i.e. you know it won’t throw an exception or you want it to if it does) then you can use @SuppressWarnings("unchecked"):

    @Override
    public void update(Observable o, Object arg) {
        @SuppressWarnings("unchecked")
        ArrayList<Message> aMessageList = (ArrayList<Message>) arg;
    }
    

    If you need to get only the messages and you don’t want exceptions:

    @Override
    public void update(Observable o, Object arg) {
        ArrayList<Message> messages = new ArrayList<Message>();
        if (arg instanceof ArrayList<?>)
            for (Object o : (ArrayList<?>) arg)
                if (o instanceof Message)
                    messages.add((Message) o);
    
        // Do stuff with the messages list
    }
    

    This will get only the Message objects from arg if it’s an ArrayList, otherwise it will be empty.

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

Sidebar

Related Questions

I'am currently working on GWTs Activity-Place implementation. Now I have found problem with the
I have a problem with the following implementation of hook_cron in Drupal 6.1.3. The
I have problem with UIWebView delay when the load image from url. In my
I have a problem with my Objective C code and an SQLite implementation. The
I have been working on the implementation of buttons to update a textview within
I am having trouble figuring out one implementation problem, I have one class, it
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with show or hide form in Window Form Application. I start

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.