after reading through generics info attempting simple sample , encountering following error.
myclass.java:32: error: cannot find symbol system.out.println("x = " + temp.x); ^ symbol: variable x location: variable temp of type t t type-variable: t extends object declared in method trythis(t) 1 error without reference "temp.x" compiles leads me believe definition correct possibly way variable referenced issue. or actual implementation wrong. not sure.
the main class has method can called either of 2 inner classes. when called, method attempts access variable specific inner class called it.
public class myclass { public class innerclass1 { int x = 100; public void runthis() { trythis(this); return; } } public class innerclass2 { int x = 200; public void runthis() { trythis(this); return; } } public static void main(string[] args) { myclass x = new myclass(); } private <t> void trythis(t temp) { system.out.println("x = " + temp.x); } }
symbol: variable x location: variable temp of type t t type-variable: t extends object declared in method trythis(t) ^^^^^^^^^^^^^^^^ without further specification, e.g. <t extends innerclass1>, thing known t within method extends object, , object, attribute x not defined.
maybe should define common super-class 2 classes, , declare x in super-class.
Comments
Post a Comment