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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:07:04+00:00 2026-06-02T16:07:04+00:00

I have this class, just for the purpose of learning: public class MyClass{ //Looking

  • 0

I have this class, just for the purpose of learning:

public class MyClass{ //Looking for a solution without making my class also generic <Type>  

    //Private Arraylist var to hold the value called myvar 

   public MyClass(ArrayList<MyDesiredType> incoming) {
        //CODE   myVar=incoming  
    }

    public MyDesiredType getType() {
        return myVar.get(0);
    }   
}

Is there any way to infer in the incoming object from the constructor to the return type of the method without warnings and castings and loosing typesafeness, but most of all WITHOUT making the whole class GENERIC (seems redundant to me)? If not, why should I think this is not feasible for the compiler?

This is a reformulated question I already did, but it was my first one and I learned how to expose it clear because nobody understood. I tried to edit later the original question but everything was buried. I changed and simplified the example and try to put it easy. Original question: Java Generics Silly Thing (Why cant I infer the type?).

If there is any problem just tell it to me and I will remove it.

  • 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-02T16:07:06+00:00Added an answer on June 2, 2026 at 4:07 pm

    No, there is not. How would the compiler know what type to return? The generic type of ArrayList in the constructor will not be known during compile time. You either have to make the whole class generic or take another approach.

    Consider this:

    public class Test {
        public static void main(String[] args) {
            List<String> arrList = new ArrayList<String>();
            arrList.add("FOO");
            Test test = new Test(arrList);
            String testStr = test.returnWhat();
            System.out.println("testStr");
        }
    
        private final List myList; //warning
    
        public <T> Test(List<T> ttype) {
            myList = ttype;
        }
    
        public <T> T returnWhat() {
            return (T) myList.get(0); //warning
        }
    }
    

    This works but gives you warnings on the marked lines. So, really there is no way to achieve what you are describing without making the whole class generic.
    Because, what if:

    public class Test {
    
    
     public static void main(String[] args) {
            List<String> arrList = new ArrayList<String>();
            arrList.add("FOO");
            Test test = new Test(); // now what?
            String testStr = test.returnWhat(0); // no warning...
            JPanel p = test.returnWhat(0); // goes through without warning, real nice...
            test.returnWhat(0); // returns Object
    
            Test test2 = new Test(arrList);
            test2.addElement(new Object()); // boom, inserted object into list of string.
            String nono = test2.returnWhat(1); // the universe goes down. assign an object to string without warning. even
                                               // though one COULD think the class is generic.
        }
    
        // private List<T> myList = new ArrayList<T>(); compiler error, T is unknown
        private List myList = new ArrayList();
    
        public Test() {
            myList.add(new Object());
        }
    
        public <T> Test(List<T> ttype) {
            myList = ttype;
        }
    
        public <T> T returnWhat(int index) {
            return (T) myList.get(index);
        }
    
        public <T> void addElement(T el) {
            myList.add(el);
        }
    }
    

    The second one doesn’t compile when myList is made generic. How could the compiler determine the type of <T> in the case where the default constructor is used?

    Further, this could lead to serious problems with Objects in collections that rely on the fact that only certain types are inserted.

    This will generate the following exception:

    Exception in thread "main" java.lang.ClassCastException:
    java.lang.Object cannot be cast to java.lang.String     at
    Test.main(Test.java:27)
    

    Did I manage to convince you?

    Real nice question, btw. I had to think about this one quite a bit.

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

Sidebar

Related Questions

I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
I'm just wondering .. I have and object like this public class Entry{ public
This is a MVC 3 project. Just for testing, I have public class MyRoleProvider
Sorry for my bad English. Just like what I asked. I have this class
I have a class just to pull data from a URL. This is not
I have this class public class Overloaded { public void ComplexOverloadResolution(params string[] something) {
I have this class called Table: class Table { public string Name { get
I have this class. public class Foo { public Guid Id { get; set;
i do this in the education purpose. such is idea: i have class Array.
I have the following code in a class of mine. The purpose of this

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.