1

I've created a class that contains a Long attribute with a setter. When trying to use the setter to set the value to some random number, Idea tells me that actual argument int cannot be converted to Long by method invocation conversion.

This is my class:

public myClass() {

    private Long id;

    public Long getId() {
        return this.id;
    }

    public Long setId(Long id) {
        if(this.id == null)
            this.id = id;
        else throw new InvalidOperationException("Can't set more than once.");
    }

}

And somewhere else, I'm just trying to:

MyClass myInstance = new myClass();
myInstance.setId(15);

The build error hinted me to try a trick like this:

long newID = 17;
myInstance.setId(newID);

...which works. Only weird thing is, I have a different project open in NetBeans, and there's no compile error in an identical situation (and it's pretty safe to rule out any "outer" influences or unwanted interactions, it's all as simple as my code snippet here).

Could this be a compiler settings thing? I'd like to now a bit more about what's going on and why can't I just use myInstance.setId(15)

oli.G
  • 1,300
  • 2
  • 18
  • 24
  • 7
    Try `myInstance.setId(15L)`; when you pass in 15 without the suffix then you're passing in an integer – Zim-Zam O'Pootertoot Apr 30 '13 at 19:56
  • [Related](http://stackoverflow.com/questions/6834037/initialize-a-long-in-java) – ajp15243 Apr 30 '13 at 19:58
  • Thanks, this works. What kind of sorcery is this? I'd never think of that, and why could it be NetBeans doesn't need this? – oli.G Apr 30 '13 at 19:58
  • 3
    It's [this sorcery](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). – ajp15243 Apr 30 '13 at 19:58
  • 2
    Your "identical" situation - just how identical is it? That sounds unlikely. Can you post a short but complete program which looks the same but *does* compile? – Jon Skeet Apr 30 '13 at 19:59
  • It's in a demo project from a school course on Java. Basically just one business entity, a CRUD manager and a test class for the manager. Here: http://chopapp.com/#cv7b20om The test class contains a whole lot of other tests which I've left out, and the Grave class contains some other attributes with trivial getters and setters. So I'd say this snippet is fairly complete. And it does compile in NetBeans. Mine has the SAME logic in the test and Manager, only the business entity is VideoGame instead of Grave, and it looks like this: http://chopapp.com/#v5j1he79 It does not compile in Idea. – oli.G Apr 30 '13 at 20:36

2 Answers2

7

Try

myInstance.setId(15L);

When you use the long newID = 17; it knows it's expecting a long, when you do myInstance.setId(15);, it doesn't and so you need to be explicit.

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
1

If you are at all willing to pass an int then just do this edit.

int a=15;
myInstance.setId((long)a);

As Zim-Zam O'Pootertoot said a number by default is an int, hence you need to explicitly cast it to a long.