Bhargav Reddy
Bhargav Reddy

Reputation: 1

I am using Object superclass in my code but it spits out an error constructor(int int) undefined

I created a class named Huh2. Then I defined Object types. It spits out an error saying: "The constructor Huh2(int, int) is undefined" when I run it.

Here is my code:

public class Huh2 {

    Object one;
    Object two;
    String trip;

    public Huh2(Object a, Object b)
    {
        one = a;
        two = b;
    }

    public void setone(Object a) {one=a;}
    public void setTwo(Object b) {two=b;}
    public Object getOne() {return one;}
    public Object getTwo() {return two;}
    public String getS() {return trip;}

    public static void main(String[] args) {
        Huh2 ii = new Huh2(1,2);
        Object i = ii.getTwo();
        System.out.println(i);
    }
}

Upvotes: 0

Views: 74

Answers (4)

Yazan Jaber
Yazan Jaber

Reputation: 2068

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The constructor Huh2(int, int) is undefined

at Huh2.main(Huh2.java:20)

It seems you are compiling with Java 1.4 (maybe compliance level set to 1.4 in the IDE/maven). If you compile with a newer version there won't be a problem because of the autoboxing feature introduced in Java 5. Here is a short description from The Java Tutorial

Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:

Passed as a parameter to a method that expects an object of the corresponding wrapper class. Assigned to a variable of the corresponding wrapper class.

Upvotes: 1

Anshul Sharma
Anshul Sharma

Reputation: 3522

add this to your class

public Huh2(){
}

If you write any constructor, then compiler does not provided default no-arg constructor. You have to specify one. I think this will help you..

Upvotes: -2

Ravikumar Rathod
Ravikumar Rathod

Reputation: 129

if your are using Object class as normal primitive data type then it throw error because it's invalid type compatible with parameter, use wrapper class case of Object

Upvotes: 2

this here is the reason:

Huh2 ii=new Huh2(1,2);

the compiler can not promote the primitive to the equivalent wrapper class

pass objects intead of primitives

Huh2 ii=new Huh2(new Integer(1), new Integer(2));

Upvotes: 1

Related Questions