• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic not recognised

 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave McIntyre
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
intGen.add(Integer(10));
should be
intGen.add(new Integer(10));
and then it will compile and run
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message is
Exception in thread "main" java.lang.NoClassDefFoundError: myGeneric/java

After changing to new Integer(10);
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks bert for your valuable advise
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic