473,386 Members | 1,652 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,386 software developers and data experts.

Generic Class and Default constructor

dmjpro
2,476 2GB
hey i m new in Generic Class in java.

look at this code carefully ....

Expand|Select|Wrap|Line Numbers
  1. class GenerericClass<T>
  2. {
  3.   private T d1,d2;
  4.   public GenericClass()
  5.   {
  6.     d1 =100;
  7.     d2 = 200;
  8.   }
  9. }
  10.  
another part of my code ....

Expand|Select|Wrap|Line Numbers
  1. GenericClass<String> c = new GenericClass<String>();
  2. //It will flash a compiler error .....
  3.  
So my conclusion is that having a default constructor in GenericClass is meaningless.
Right????

i think experts allof u get my point .....
so plz explain ......

kind regards,
Dmjpro.
Jun 14 '07 #1
8 5329
JosAH
11,448 Expert 8TB
hey i m new in Generic Class in java.

look at this code carefully ....

Expand|Select|Wrap|Line Numbers
  1. class GenerericClass<T>
  2. {
  3.   private T d1,d2;
  4.   public GenericClass()
  5.   {
  6.     d1 =100;
  7.     d2 = 200;
  8.   }
  9. }
  10.  
If d1 and d2 are of a generic type T you can't assume that they can contain
numbers (Short, Integer, Long, Float or Double). That code is simply wrong and
has nothing to do with the fact that you assume that in a default constructor,
i.e. it is wrong everywhere in a generic class.

kind regards,

Jos
Jun 14 '07 #2
r035198x
13,262 8TB
hey i m new in Generic Class in java.

look at this code carefully ....

Expand|Select|Wrap|Line Numbers
  1. class GenerericClass<T>
  2. {
  3.   private T d1,d2;
  4.   public GenericClass()
  5.   {
  6.     d1 =100;
  7.     d2 = 200;
  8.   }
  9. }
  10.  
another part of my code ....

Expand|Select|Wrap|Line Numbers
  1. GenericClass<String> c = new GenericClass<String>();
  2. //It will flash a compiler error .....
  3.  
So my conclusion is that having a default constructor in GenericClass is meaningless.
Right????

i think experts allof u get my point .....
so plz explain ......

kind regards,
Dmjpro.
DJ, Your GenerericClass won't compile for two reasons.
1.) The "constructor"'s name is different from the class' name and you hav e
d1 =100;
d2 = 200;
which are not allowed since d1 and d2 are objects of a type T which is incompatible with int.
Jun 14 '07 #3
dmjpro
2,476 2GB
If d1 and d2 are of a generic type T you can't assume that they can contain
numbers (Short, Integer, Long, Float or Double). That code is simply wrong and
has nothing to do with the fact that you assume that in a default constructor,
i.e. it is wrong everywhere in a generic class.

kind regards,

Jos

Yup ... u r right.
Actually wat happens .... without compiling the GenericClass i wrote what was in my mind.
now if i want to add some default values in default constructor in generic class then what to do?
plz suggest.

kind regards,
Dmjpro.
Jun 14 '07 #4
JosAH
11,448 Expert 8TB
Yup ... u r right.
Actually wat happens .... without compiling the GenericClass i wrote what was in my mind.
now if i want to add some default values in default constructor in generic class then what to do?
plz suggest.

kind regards,
Dmjpro.
If those defaults have a fixed type then those members aren't generic (as in your
example class). If they *are* generic all you know is that you can set them to
something with the same generic type or null. Those numbers 100 and 200 most
certainly aren't generic. You have to redesign your class.

kind regards,

Jos
Jun 14 '07 #5
dmjpro
2,476 2GB
If those defaults have a fixed type then those members aren't generic (as in your
example class). If they *are* generic all you know is that you can set them to
something with the same generic type or null. Those numbers 100 and 200 most
certainly aren't generic. You have to redesign your class.

kind regards,

Jos

ohh that means i have to believe in default values set by the JVM.
right???

kind regards,
Dmjpro.
Jun 14 '07 #6
r035198x
13,262 8TB
ohh that means i have to believe in default values set by the JVM.
right???

kind regards,
Dmjpro.
I don't see where the JVM comes into this.
Jun 14 '07 #7
dmjpro
2,476 2GB
I don't see where the JVM comes into this.
Nooo . i just say generic solved by the compiler but ultimate job done by JVM.
that's why i telling....

Kind regards,
Dmjpro.
Jun 14 '07 #8
JosAH
11,448 Expert 8TB
Nooo . i just say generic solved by the compiler but ultimate job done by JVM.
that's why i telling....

Kind regards,
Dmjpro.
If you want a member variable to be equal to 100 or 200, that particular member
variable can't be part of the generic contract. It simply is a number.

kind regards,

Jos

edit: here's a nice tutorial.
Jun 14 '07 #9

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

Similar topics

7
by: Master of C++ | last post by:
Hello Folks, I have a programming dilemma with templates and "genericity". I can think of a few solutions, but I am not sure which one is the best in terms of genetic C++ style. Any suggestions...
2
by: yaniv.golan | last post by:
Hi, Either I am missing something, or there is a compiler bug in the way inheritance from generic s is treated. In the following code, class c2 inherits from c1_generic. c1_generic has T as a...
3
by: SammyBar | last post by:
I'm trying to inherit from a Generic class but I'm having troubles on the syntax on how to specify a base constructor for my default constructor. I want to do something like this: public class...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
11
by: Tony Maresca | last post by:
I'm having a hard time understanding why I get the following error with the code shown. Can someone explain? public ref class Class1 { }; generic<typename Twhere T: Class1, gcnew()
7
by: Andrus | last post by:
public class BusinessObjectGeneric<EntityType: BusinessObject where EntityType : BusinessEntity, new() { public BusinessObjectGeneric<EntityType() { } ..... causes error in constructor...
2
by: gilbert | last post by:
Hello. I am trying to use c# generic to define a class. class MyClass<Twhere T: new(){ } In this definition, MyClass can create T objects with a default constructor. Is there any way to...
6
by: Jon | last post by:
Hi, When I try to compile the following generic class, the compiler gives me many errors "Cannot conver type '...' to '...'" on the lines indicated. Besides, the C# compiler gives me errors even...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.