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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:09:19+00:00 2026-06-09T09:09:19+00:00

public static <T> T inCache(T obj) throws ClassNotFoundException { String[] token = validateCookie(); //gives

  • 0
public static <T> T inCache(T obj) throws ClassNotFoundException {
        String[] token = validateCookie(); //gives me to strings
        if (token == null)
            return null;
        if (Cache.get(token[0]) != null) {
            if (Cache.get(token[0]).getClass() == Class.forName(token[1])
                    && obj.getClass() == Cache.get(token[0]).getClass()) {
                T test = (T) Cache.get(token[0]);
                return test;
            }
        }
        return null;
    }

The code above is completely wrong.

Basicly I want to do something like this:

  • I want to set the class in my function. for example inCache<User>();
  • check if the object that i get out of my cache has the same class that i have specified before. (obj.getClass == User.class)

  • If the classes matches , cast the object to the class and return it. return (User)obj

I want to use it like this.

User user = inCache<User>();
  • 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-09T09:09:21+00:00Added an answer on June 9, 2026 at 9:09 am

    As for the class signature, why don’t you use something like this:

    public static <T> T inCache(Class<T> clazz) throws ClassNotFoundException {
      ...
    }
    

    And then call it like this:

    User user = inCache(User.class);
    

    Generics can’t be used the way you described (User user = inCache<User>();) due to type erasure at runtime, i.e. the type of T is unknown at runtime in that case.

    Also note that it might be better to test using Class#isAssignableFrom(...) to be able to check for subclasses as well, e.g. clazz.isAssignableFrom(Cache.get(token[0]).getClass()). That way you could pass an interface or super class and still get a match if the object is of a subtype.

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

Sidebar

Related Questions

public static Node createNodeFromXMLString(String xml) throws SAXException, IOException { return builder.parse(new ByteArrayInputStream(xml.getBytes())) .getDocumentElement(); }
public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
public static string GetCurrentEmployeeName() { return db.Employees.SingleOrDefault( c => c.NTUserName == CurrentUsername() ).Last_First; }
public static String ConnDB() throws ParseException, IOException { // TODO Auto-generated method stub String
Am using following .net code to add objects to cache: public static void Add<T>(string
public static DateTime ResolveDate() { return null; } Im required to return a null
public static void setMOTD(final String motd) throws Exception { final Field field = MinecraftServer.class.getDeclaredField(motd);
public static void main(String args[]) throws IOException { Process p = Runtime.getRuntime().exec(java E:/workspace/JNIProgram/src/JNIProgram.class); }
public static string ProductHelper(this Product p) { // Need to get the DisplayName value
public static void getMembers(WebDriver driver, String outname){ FileWriter outFile = null; try { outFile

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.