1

Non-reifiable types are types where information has been removed at compile-time by type erasure

so I am expecting that after compilation the type-information of generics is lost.

package com.v;

import java.util.ArrayList;
import java.util.List;

public class NonReifiable {
    
    List<Human> hl = new ArrayList<>();
    
    List<NonHuman> nhl = new ArrayList<>();
    
}

class Human { }    
class NonHuman {}

But, I see the .java generated from the compiled .class still has type info.

// 
// Decompiled by Procyon v0.5.36
// 
package com.v;

import java.util.ArrayList;
import java.util.List;

public class NonReifiable
{
    List<Human> hl;
    List<NonHuman> nhl;
    
    public NonRefiable() {
        this.hl = new ArrayList<Human>();
        this.nhl = new ArrayList<NonHuman>();
    }
}

How is the above possible. Could some one help me better understand how it work practically.

This is what I have been expecting

public class NonReifiable
{
    List<Object> hl;
    List<Object> nhl;

    public NonRefiable() {
        this.hl = new ArrayList<Object>();
        this.nhl = new ArrayList<Object>();
    }
}
samshers
  • 1
  • 6
  • 37
  • 84
  • Non reifiable means the type information is not available at runtime. It being in the class file does not contradict that fact. – Sweeper Dec 04 '20 at 09:47
  • See [this](https://stackoverflow.com/a/937987/5133585). – Sweeper Dec 04 '20 at 09:51
  • @Sweeper, the ref says `so there is no type information in the bytecode`. Is not `.class` file a bytecode file. – samshers Dec 04 '20 at 10:09
  • the quote `Non-reifiable types are types where information has been removed at compile-time by type erasure` is from oracle official site... so clearly it says removed at compile-time. Will appreciate if you can pitch in. – samshers Dec 04 '20 at 10:12
  • 2
    A .class file contains byte code, and other things, namely the _constant pool_ where the generic type arguments are found, as seen in the linked answer. That quote seems to be from a Oracle _Tutorial_, which simplifies a lot of things for pedagogical purposes. Look at the [JVM spec](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.9): `A Signature attribute records a signature for a class, interface, constructor, method, or field whose declaration in the Java programming language uses type variables or parameterized types.` – Sweeper Dec 04 '20 at 10:18
  • thats it, much appreciated. tx. Should be a answer worth accepting if you wish. – samshers Dec 04 '20 at 10:20

0 Answers0