Cannot initialize Integer variable

This is a self-answered question!

I'm trying to initialize an Integer variable with a value, but it's not working as expected. It gives me different errors.

What I've tried so far:

public class Integer {
    public static void main(String[] args) {
        Integer num1 = 42;               // Type mismatch: cannot convert from int to Integer
        Integer num2 = (Integer)42;      // Cannot cast from int to Integer
        Integer num3 = new Integer(42);  // The constructor Integer(int) is undefined
    }
}

What's the issue here? How can I set the value of my Integer properly?

Answers


Try not to use same names of built-in classes!

Since you're using the same name of the class java.lang.Integer, you're hiding the built-in class with your own. So, if you're accessing an Integer in your example, you're accessing your own class that is not the built-in java.lang.Integer class of the JDK.

The first and the second error occur, because your class is not compatible with a real java.lang.Integer. The third error occurs, because you don't have a constructor in your Integer class that accepts an int as parameter.

So the easiest way to get rid of the errors is just by naming your class different. For example, name it IntegerTest or something that fits your intention.


Consider the following example of a really bad practice of using the name Integer in different cases:

package Integer;

class Integer {
    public static void main(String[] lnteger) {
        System.out.println(Integer(new Integer(42)).Integer);
    }

    java.lang.Integer Integer;

    Integer(java.lang.Integer Integer) {
        this.Integer = Integer;
    }

    static Integer Integer(Integer lnteger) {
        Integer Integer; Integer:
        for (Integer = lnteger; Integer.Integer < Integer.new lnteger(lnteger).Integer.Integer; Integer.Integer++) {
            Integer(new Integer(--lnteger.Integer));
            continue Integer;
        }
        return lnteger;
    }

    class lnteger {
        Integer Integer;
        lnteger(Integer Integer) {
            this.Integer = Integer;
        }
    }
}

Unfortunately, this compiles.

You're not really able to distinguish between the different kinds of Integer. Without an IDE you're lost. With an IDE you're probably lost, too.

Even the formatter from SO can not display this in a good manner since Integer is a package name, class name, member name, parameter name, method name, and label name.

So give all things a meaningful (and at best unique) name to avoid such confusion.


Need Your Help

Bootstrap .pull-right doesn't affect .navbar-search

css twitter-bootstrap

I would like to use the .navbar-search class in Boostrap, but I would like to use it with .pull-right. However .pull-right doesn't seem to affect the search bar as .navbar-search has a float: left...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.