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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:25:05+00:00 2026-06-13T10:25:05+00:00

I have a method from which i am returning object like public static Object

  • 0

I have a method from which i am returning object like

public static Object login(DataManager dataManager, String userName, String password)       
    ArrayList<LoginCredentialsBean> loginCredentialsList = new ArrayList<LoginCredentialsBean>();       
    String authenticated = "false";     
    Connection connection = null;       
    try {           
        connection  = dataManager.getConnection();          
    } catch (Exception e) {         
         return ("Having problem in connectiong to databaste: " + e.getMessage());          
    }       
    if (connection != null) {           
         try {                                       
             ...                     
             try {                   
                 ResultSet rs = prepStatement.executeQuery();                   

                 try {                      
                     while (rs.next()) {
                         ...                             
                         loginCredentialsList.add(new LoginCredentialsBean(roleId, orgaCode, passwordExpiryDate, date, status, language));                                                          
                        authenticated = "true";                          
                     } //end of while()                                                  
                 } finally {                     
                     rs.close();                     
                 }                   
             } finally {
                 prepStatement.close();                  
             }                
             if (authenticated.equals("true")) {                     
                updateUserLogByInserting(connection, userName);                  
             }

         } catch(SQLException e) {               
             System.out.println("Could not login from dataabse:" + e.getMessage());              
         } finally {                 
             dataManager.putConnection(connection);                          
         }                  
    }       
    if (authenticated.equals("true")) {                 
        return loginCredentialsList;            
    } else {        
        return authenticated;           
    }       
} //end of login()

Now i am testing it like

public static void main(String... args) {

    MoneyTreeServices moneyTreeServices = new MoneyTreeServices();

    Object result = moneyTreeServices.login("Admin", "cbas1234");       
    if (result instanceof ArrayList<?>) {

        System.out.println("ArrayList instance");           
    }       
    System.out.println(result);
}

It returns me result like

ArrayList instance
[pk.mazars.moneyTree.bean.LoginCredentialsBean@b7ec5d]

I want to ask i am using condition like ArrayList<?>. How can i check that ArrayList that contain LoginCredentialsBean object. Like when i use

if (result instanceof ArrayList<LoginCredentialsBean>) {

}

i get error that

Can not perform instanceof check against parameterized type ArrayList<LoginCredentialsBean>. Use the form ArrayList<?>

I want to check instanceof ArrayList and arraylist has LoginCredentialsBean ?

Thank you.

  • 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-13T10:25:06+00:00Added an answer on June 13, 2026 at 10:25 am

    The short answer is that you can’t. Generics are implemented via type erasure – they’re effectively a compile-time syntactic sugar to ensure you don’t put an Integer into a List<String>.

    The runtime objects themselves, however, are just the raw types. An instance of ArrayList doesn’t know that it’s an ArrayList<String> (or rather, that it was assigned to a variable with that generic type). So when you interrogate it with reflection, you cannot get any generic type info.

    There are two broad types of solution I can think of. One is to iterate over the list contents and check their dynamic type – if the first element is a LoginCredentialsBean, for example, then it’s reasonable to assume that you have a List<LoginCredentialsBean>. This won’t work for empty lists though, which could be a problem, and can potentially give false positives (e.g. a List<Object> allParameters might happen to have a LoginCredentialsBean as its first element…)

    The other is to explicitly pass metadata objects around – so in this case you’d return the Object from the login method, along with a token which describes what type of object it is. This could be a simple enum constant; or going to the other extreme you could make the tokens generically typed, such that the compiler can check this against the type of what you’re returning and ensure that the tokens are type-correct.

    But in any case, instanceof is too little (information), too late.


    Mind you, your login method looks… odd. I don’t think it should return an Object at all, as that’s just lazy and completely subverting the static type system which would help you here. Rather, I think it should just return a List<LoginCredentialsBean> containing the credentials that pertain to the given login.

    You have three different paths where you return. The first is if an exception is encountered when connecting to the database – in which case you should throw an exception! Returning a string with the error details is very atypical and confusing – an exceptional condition should be handled as an Exception, that’s what they’re for.

    The other two situations are ones where you’re able to look up definitive results. For the failed login case, I would just return an empty list (i.e. this username/password has no credentials whatsoever), while returning the populated list during a successful login.

    If you strongly want to be able to distinguish between a login failure, and a successful login (with no credentials), then perhaps return a compound object instead, such as:

    class LoginStatus {
        final boolean authenticated;
        final List<LoginCredentialsBean> credentials;
    }
    

    Either way, the caller knows exactly what they’re getting back, and can call methods on it appropriately without having to call instanceof and typecast.

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

Sidebar

Related Questions

I have static method which returns me as it's name says data from domain
I have this static method in my business layer for returning Dinners public static
I have c#.net code which calls a method from another external/referenced .net assembly. This
I have created a custom shipping method to show depo-names from which customers will
I have this method which will remove all rows from a table but I
I have a method which is list my items from a server. Therefore, this
I have an extension method which I can use from the .cs codebehind of
I have a utility method which returns a strongly typed value from an old
I have the following method which is not capturing anything from the user.If I
I have a class which inherits from Canvas. On the OnRender method I draw

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.