---
title: "How can I auto box this class?"  
description: "How can I auto box this class?"  
author: "Andrew Deniel"  
published: 2013-10-05  
updated: 2013-10-05  
canonical: https://www.mindstick.com/forum/1605/how-can-i-auto-box-this-class  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# How can I auto box this class?

I've the following [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp):

\

```
public class IntegerKey extends Number implements Comparable<IntegerKey> {
    private Integer m_key;
    public IntegerKey(Integer key) {
        m_key = key;
    }
    public IntegerKey(int key) {
        m_key = key;
    }
}
```

\

I would like to use this class as Follow:

assume I have the following [generics](https://www.mindstick.com/articles/12420/generics-in-c-sharp-with-example):

```
Map<IntegerKey, MyCache> map = new HashMap<IntegerKey, MyCache>();
map.put(5, new MyCache());
```

\

This doesn't [compile](https://www.mindstick.com/forum/2316/how-to-views-compile-in-mvc), why?? I don't want to do:

```
map.put(new IntegerKey(5), new MyCache());
```

Thank you!

## Replies

### Reply by Anonymous User

Autoboxing only works for the primitives types and their respective counter parts in java.lang. In your example you could try dropping IntegerKey altogether and simply use Integer.


---

Original Source: https://www.mindstick.com/forum/1605/how-can-i-auto-box-this-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
