Please consider the following question and answer taken from KB Oracle Practice Exams book:
Question:Which can be inserted, independently at line 4, for the code to compile? (Choose all that apply.)
A. return e;
B. return e.getE();
C. return e2;
D. return e2.getE();
E. return new Hose().getE();
F. Compilation fails regardless of which return is inserted.
Answer:F is correct. The generic type "E", which is declared at the class level, will be associated with each instance of Hose, and is not accessible to static methods. If doStuff() was non-static, A and D would be correct.
My doubt is if doStuff() method is non-static, is the choice B correct together with A and D?
I tried removing the 'static keyword' and tried the choice B, that is "return e.getE();", but it doesn't compile. I am getting the error "cannot convert from Hose to E". The method e.getE() also returns a E, then why it is not working.
Could someone please explain this.
Thanks and regards
Loganathan. K
Question:Which can be inserted, independently at line 4, for the code to compile? (Choose all that apply.)
A. return e;
B. return e.getE();
C. return e2;
D. return e2.getE();
E. return new Hose().getE();
F. Compilation fails regardless of which return is inserted.
Answer:F is correct. The generic type "E", which is declared at the class level, will be associated with each instance of Hose, and is not accessible to static methods. If doStuff() was non-static, A and D would be correct.
My doubt is if doStuff() method is non-static, is the choice B correct together with A and D?
I tried removing the 'static keyword' and tried the choice B, that is "return e.getE();", but it doesn't compile. I am getting the error "cannot convert from Hose to E". The method e.getE() also returns a E, then why it is not working.
Could someone please explain this.
Thanks and regards
Loganathan. K