-1

Place code into the class so that it compiles & generates the out put answer=42

Note:Code options may be used more than once.

enter image description here

This question is from SCJP , I have posted answer. I can't understand why they used

public Gen (T object){this.object = object}  

instread of

public T ( T object){this.object = object}

i am getting confused in this. Please help me out

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
user2985842
  • 437
  • 9
  • 24

4 Answers4

0

public Gen (T object){this.object = object} is the constructor.

The constructor MUST have the exact same name as the class

geoand
  • 60,071
  • 24
  • 172
  • 190
0

Because gen is the class name here, where T is the generic. So Its class Gen with object type T. If you said class T then T would be the class name and not the object type.

javawocky
  • 899
  • 2
  • 9
  • 31
0

Because public Gen (T object){this.object = object} is the constructor of the class Gen

TheEwook
  • 11,037
  • 6
  • 36
  • 55
0

In the constructor signature, the constructor name, must match the class name. Also, why T would represent Gen type?. The whole idea is getting compile type safety when using statements like Gen<String>, Gen<Integer>...

Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44