LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 0
java - generic variables


Hi,
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:
Code:
public class GenIFDemo { 
  public static void main(String args[]) { 

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

  } 
}
and two more classes:
Code:
interface MinMax<T extends Comparable<T>> { 
  T min(); 
  T max(); 
}
Code:
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, 05:14 PM   #2
vladmihaisima
Member
 
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).
 
  


Reply


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 04:31 AM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 01:30 PM
Setting environment variables in Java linuxprogrammer Linux - General 2 03-03-2005 06:09 PM
Java : Meta variables ? pycoucou Programming 7 04-16-2004 04:46 AM
environment variables with java lilrowdy18 Linux - Software 1 08-20-2003 06:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 AM.

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