Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-12-2006, 05:12 AM   #1
LQ Newbie
Registered: Nov 2005
Posts: 6

Rep: Reputation: 0
java - generic variables

I've got a problem using a class MyClass as a generic variable, like this: MyClass<MyClass> iob = new MyClass<MyClass>();

For example, the main:
public class GenIFDemo { 
  public static void main(String args[]) { 

    MyClass<MyClass> iob = new MyClass<MyClass>(); 

and two more classes:
interface MinMax<T extends Comparable<T>> { 
  T min(); 
  T max(); 
class MyClass<T extends Comparable<T>> implements MinMax<T>, Comparable<T> { 
  T[] vals; 
  MyClass() { int k = 0; } 
  // Return the minimum value in vals. 
  public T min() { 
    T v = vals[0]; 
    for(int i=1; i < vals.length; i++) 
      if(vals[i].compareTo(v) < 0) v = vals[i]; 
    return v; 
  // Return the maximum value in vals. 
  public T max() { 
    T v = vals[0]; 
    for(int i=1; i < vals.length; i++) 
      if(vals[i].compareTo(v) > 0) v = vals[i]; 
    return v; 
  public int compareTo(T o)
    return 1;
This is just an example, it's no finished (the compareTo(), for example, doesn't have sense) but I just want to know why it doesn't compile. When I try to compile it I got two errors saying: "type parameter MyClass is not within its bound".
I've already searched with google, and the problem always is a missing 'implements', but I think that every possible 'implements' is already in the code.
What am I doing wrong??

Thx for help
Old 03-12-2006, 04:14 PM   #2
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
I think you should reread some tutorial on generics.

And after that, think about what you want to accomplish with the code (if you are focused on that you will probably write better code). From what I see you want a class that implements some sort of vector of a 'generic' type. This would be usefull for example if you would want a vector of int-s and a vector of float-s. Instead of writing two classes you write just one 'generic' one and create classes for each of the types.

"MyClass<MyClass> iob" is incorrect as the type between < and > is not completely specified (MyClass is a generic class, it needs another type specified when used).

You could say:

MyClass<int> i
MyClass<float> f

or even

MyClass<MyClass<int>> mi (this would mean a vector of vectors of int).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
JAVA: Reference Variables (Pointers) and Constructors wwnexc Programming 14 03-10-2006 03:31 AM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
Setting environment variables in Java linuxprogrammer Linux - General 2 03-03-2005 05:09 PM
Java : Meta variables ? pycoucou Programming 7 04-16-2004 03:46 AM
environment variables with java lilrowdy18 Linux - Software 1 08-20-2003 05:43 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:16 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration