| Author |
generic not recognised
|
raja kanak
Ranch Hand
Joined: Oct 18, 2006
Posts: 135
|
|
The following code doesn't recognise generic keyword itself. Im using jdk1.5.0_06. Or should I import which package? public class myGeneric<T> { private T t; public void add(T t){ this.t = t; } public T get(){ return t; } public static void main(String[] arg){ myGeneric<Integer> intGen = new myGeneric<Integer>(); intGen.add(Integer(10)); Integer someInt = intGen.get(); System.out.println(someInt); } thanks in advance
|
live
|
 |
Dave McIntyre
Greenhorn
Joined: Oct 22, 2006
Posts: 8
|
|
intGen.add(Integer(10)); should be intGen.add(new Integer(10)); and then it will compile and run
|
 |
raja kanak
Ranch Hand
Joined: Oct 18, 2006
Posts: 135
|
|
thanks Dave McIntyre. But compiler shows error at <T> in "public class myGeneric<T> {" I think it is not recognising generic Is it ok to compile the code in jdk1.5._06 or any imports are necessary?
|
 |
Andy Morris
Ranch Hand
Joined: May 30, 2004
Posts: 78
|
|
I have tested that code and it compiles once you apply Dave McIntyre's fix. Perhaps you should state the actual compiler error you're getting?
|
 |
raja kanak
Ranch Hand
Joined: Oct 18, 2006
Posts: 135
|
|
The error message is Exception in thread "main" java.lang.NoClassDefFoundError: myGeneric/java After changing to new Integer(10);
|
 |
Andy Morris
Ranch Hand
Joined: May 30, 2004
Posts: 78
|
|
That is NOT a compiler error, that is a runtime exception - quite different! However, I have just re-compiled and executed that code and it does not throw an Exception. My console prints out '10' as expected. I did have to add an extra curly brace but I assume you have pasted it in incorrectly as the error you're getting is not at compile time. I'm thinking you are not using the correct syntax when you are executing your code (e.g typing java myGeneric.class or something else incorrect). Can you paste in the class exactly as it is now and the command you are using to run it (unless you are using an IDE)?
|
 |
Andy Morris
Ranch Hand
Joined: May 30, 2004
Posts: 78
|
|
Raja, I think you need to look at some of the fundamentals before going for certification. The problem you are encountering is nothing to do with Generics. I'd bet a good number of virtual pounds you are typing in "java myGeneric.java" You need to compile your code first, e.g "javac myGeneric.java" and then run it by typing "java myGeneric" Or get an IDE to compile it for you Or use Ant [ October 23, 2006: Message edited by: Andy Morris ]
|
 |
raja kanak
Ranch Hand
Joined: Oct 18, 2006
Posts: 135
|
|
thanks guys. i am using a eclipse IDE. when i try to compile it using command line; javac myGeneric.java it compiles & when i run it using command line; java myGeneric it gives output 10. So the problem with my IDE. THANKS FOR YOUR GUIDANCE
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
Hi Guys, As always, we recommend that you use the command line to compile and run your Java programs while you're studying for the SCJP. In fact, many of the actual exam questions will either assume that or state that scenario explicitly - and none of the real exam questions will have anything to do with any IDE. hth, Bert
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
raja kanak
Ranch Hand
Joined: Oct 18, 2006
Posts: 135
|
|
|
thanks bert for your valuable advise
|
 |
 |
|
|
subject: generic not recognised
|
|
|