473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

The type is a ClassName in generics

dmjpro
2,476 2GB
I have this code ..
Please have a look...
Expand|Select|Wrap|Line Numbers
  1. class GenericClass<Number>{
  2.     Number number = null;
  3.     GenericClass(Number number){
  4.         this.number = number;
  5.     }
  6. }
  7.  
  8. GenericClass<String> gc = new GenericClass(100); //It gets compiled
  9. System.out.println(gc.number); //it leads me to a ClassCastException
  10.  
The Number is a type or ClassName?
Apr 29 '09 #1
11 2606
r035198x
13,262 8TB
Doesn't your compiler give you a warning when you compile that? Read that warning.
Apr 29 '09 #2
dmjpro
2,476 2GB
I didn't see that warning. So here Number is treated as type name?
Why warning, why not error?
Apr 29 '09 #3
r035198x
13,262 8TB
Why should it be an error? You are allowed to hide those classes at your own peril.
Apr 29 '09 #4
dmjpro
2,476 2GB
@r035198x
If i add a method like this ..

Expand|Select|Wrap|Line Numbers
  1. void add(Number number){this.number=number;}
  2.  
and then if i use this, then it flashes me an error ..

Expand|Select|Wrap|Line Numbers
  1. gc.add(200); //it shows compile error.
  2.  
Why in case of constructor it shows a warning and in case of method it shows error?
Apr 29 '09 #5
r035198x
13,262 8TB
Post the code that you used and the error that you got.

P.S You'd better read the generics tutorial first before trying to use generics.
Apr 29 '09 #6
dmjpro
2,476 2GB
@r035198x
Here is my Code ...
Expand|Select|Wrap|Line Numbers
  1. class GenericClass<Number>{
  2.     Number number = null;
  3.     GenericClass(Number number){
  4.         this.number = number;
  5.     }
  6.     void add(Number n){
  7.         this.number = n;
  8.     }
  9. }
  10.  
  11. public class Main {
  12.    public static void main(String[] args) {
  13.         GenericClass<String> gc = new GenericClass(100);//it does not show me error
  14.         gc.add(200); //it shows me error
  15.     }
  16. }
  17.  
Apr 29 '09 #7
JosAH
11,448 Expert 8TB
@dmjpro
You do realize that the class java.lang.Number is hidden by that generic class don't you? Autoboxing goes berzerk and a general mess will be yours.

Better stay far away from that dangerous crap if you don't know what you are doing.

kind regards,

Jos
Apr 29 '09 #8
dmjpro
2,476 2GB
See ... What i realized..

Expand|Select|Wrap|Line Numbers
  1. class GenericClass<T>{
  2. GenericClass(T t){...}
  3. }
  4.  
  5. GenericClass<String> gc = new GenericClass(/*Any type of object*/);
  6.  
  7. GenericClass<String> gc = new GenericClass<ClassName>(/*Only ClassName type of object*/);
  8.  
Apr 29 '09 #9
r035198x
13,262 8TB
The first line in your main should give you another warning as well.
You can't call add with numbers because you specified your type to be String. You can only add Strings to it. Better use the conventions (T for type and E for element) when using generics.

Don't forget to read that generics tutorial.
Apr 29 '09 #10
JosAH
11,448 Expert 8TB
@r035198x
IMHO it would've been much better to explicitly forbid an existing class name to be used as a generic type name; some poofters might try to come up with MyClass<String> or MyClass<Number> and be surprised that some stuff won't work as expected, especially when they want to use those (hidden) class names from within the definition of their crappy generic class.

kind regards,

Jos
Apr 29 '09 #11
r035198x
13,262 8TB
I thought as much as well the first time I saw this thread. They did put the warning though. Now they need to get people to read those warnings.
Apr 29 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
1
by: Benny Raymond | last post by:
I'm working on adding plugin capability to my application and would like to do it something like this: System.Type myType = System.Type.GetType( "namespace.classname, assembly_name" ); object...
20
by: jernej goricki | last post by:
Hi, Is it possible to do this in vb.net : 1.)To make a function that would have a string parameter ClassName 2.)This function would return an object of the same type as the ClassName parameter...
5
by: garfy | last post by:
Hi i get this error in validation Line 22 column 6: document type does not allow element "title" here. <title>Seo Web Design Los Angeles - Web Design And Search Engine Optimization L ...
5
by: Random | last post by:
How can I use reflection (or some other method) to find the type of an object that has been passed in to my method under an interface definition? I try to use GetType, but that won't work.
4
by: sklett | last post by:
I'm revisiting an old series of overloaded methods I have and I would like to convert them to a single generic method. Here is the method in it's current, overloaded implementation: <code>...
4
by: Random | last post by:
I want to define a generics method so the user can determine what type they expect returned from the method. By examining the generics argument, I would determine the operation that needs to be...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.