You are creating the instance in the "wrong way"
You are not creating an instance of your class Generic, so the compiler is accepting a different type in the constructor without error.
This way you created an instance of your class:
Grid<Empresa> grid = new Grid(...);
is permitted by the compiler due to backward compatibility. See: The Java Tutorials - Raw Types.
In short, although you have no class Grid
for what you declared was Grid<T>
, namely, a Generic, compiler considers that there is a yes class Grid
and he calls her raw type. It needs this for compatibility with old code, prior to the advent of Generics
However, how to create a class instance Generic is this:
Grid<Empresa> grid = new Grid<Empresa>(...);
new Grid<Company>(...) instead of new Grid(...)
That is, you should inform the Generic Parameter in creating the instance, otherwise you are actually creating an instance of the raw type and not an instance of your Generic class.
If you correctly create an instance of the class Generic you will have a build error when the type passed to your constructor does not match the type of the Generic Parameter.
In C# (another language to support Generics) there’s no such thing as raw type so that you are obliged to explain the Generic Parameter as I did above.
In Java you can also use the syntax called Diamond <>, letting the compiler hint at the type of Generic Parameter from the variable declaration:
Grid<Empresa> grid = new Grid<>(...);
If you follow this practice, explaining the Generic Parameter or using the syntax Diamond, a build error will be generated if you try to use an instance of type incompatible with Generic Parameter informed.
Why Netbeans Warns Against Bad Practice?
Because the Warning compiler regarding the use of raw types is turned off by default. If you look at the hints compiler, however, the warning will be there:
Note: Main.java uses unchecked or unsafe operations.
If you activate this Warning in Netbeans (in Eclipse it is enabled by default), you will be notified by the editor and also during compilation with the following message:
Grid is a raw type. References to generic type Grid
should be parameterized.
Type safety: The constructor Grid(Integer, List) belongs to the raw type Grid.
References to generic type Grid should be parameterized.
You could even treat this Warning as an error, preventing compilation when an instance of a raw type.
I don’t have Netbeans installed but this image shows more or less where this setting is:
Loco. Are you sure about this? I don’t know Java so deep to talk about it, but for me this form is correct and should restrict. I imagine I’m wrong and someone will tell you what to do.
– Maniero
This is the correct form. Perhaps you are wondering why the IDE does not see it as a compilation error, which in fact it is not. Try to run your program exactly as shown and you will see that you will be released an exception referring to the type reported to be different.
– user28595
@Diegofelipe, just instantiating in this way did not give error neither compilation nor execution. However, when trying to fetch an item from the list, it gave Cast error, which is correct...
– Franchesco
You see you’re wearing
List
and notArrayList
? Anyway how about doing an MCVE?– Maniero
@bigown believe this is not even the problem, because Arraylist implements List, so it is not incorrect. From what I understand, he wants to block in the constructor pass a list of a different kind, the only way I know how to do this is using interface, but since I don’t really understand interfaces, maybe someone with more experience can explain better.
– user28595
I don’t think so and I don’t think interface has anything to do with it.
– Maniero
@Earendul take a look at this file, see if this addresses your problem. http://www.inf.pucrs.br/flash/alpro2/present/U03_projeto/01b-genericos/handout.html#generic
– user28595
I did a test and it really happens. What a mess. I did it in C# and it doesn’t even compile because the guy is wrong. Now I want to see how to solve this.
– Maniero
The Eclipse gives the following information
Type safety: The expression of type Grid needs unchecked conversion to conform to Grid<Empresa>
innew Grid(10, listaUsuarios);
– ramaral
@Diegofelipe read the file, but still do not know how to solve the problem. Maybe only even avoiding receiving the list in the constructor and only in the set.
– Franchesco
@ramaral And Netbeans warns nothing..
– Franchesco
@I also want to see how you solve this, :)
– Franchesco
Possible cause of the problem: http://stackoverflow.com/questions/9366121/calling-constructor-of-a-generic-type From what I understand, it says that the moment the class is instantiated, the constructor is called, but when a Generic type is inferred in this class, the type so far is unknown, so java has no way to check the type T because until the constructor is finished, this type is unknown.
– user28595
@bigown if your English is good, please check this topic on Soen, by instant translation, seems to answer this case.
– user28595
@Diegofelipe this I had already realized, because if I could, they would, I even wondered what was the reason since they use type Erasure. What I want to know is how to solve this. I imagine is to do what is done in dynamic language. And some people say that Java is a safe language in types, especially when you buy with C#.
– Maniero
A light at the end of the tunnel http://stackoverflow.com/questions/1942644/get-generic-type-of-java-util-list/ I just can’t explain why unfortunately I haven’t learned java reflection yet (although this topic is a challenge for such hehe).
– user28595