hey i m new in Generic Class in java.
look at this code carefully .... -
class GenerericClass<T>
-
{
-
private T d1,d2;
-
public GenericClass()
-
{
-
d1 =100;
-
d2 = 200;
-
}
-
}
-
another part of my code .... -
GenericClass<String> c = new GenericClass<String>();
-
//It will flash a compiler error .....
-
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.
8 5329
hey i m new in Generic Class in java.
look at this code carefully .... -
class GenerericClass<T>
-
{
-
private T d1,d2;
-
public GenericClass()
-
{
-
d1 =100;
-
d2 = 200;
-
}
-
}
-
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
hey i m new in Generic Class in java.
look at this code carefully .... -
class GenerericClass<T>
-
{
-
private T d1,d2;
-
public GenericClass()
-
{
-
d1 =100;
-
d2 = 200;
-
}
-
}
-
another part of my code .... -
GenericClass<String> c = new GenericClass<String>();
-
//It will flash a compiler error .....
-
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.
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.
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
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.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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()
|
by: Andrus |
last post by:
public class BusinessObjectGeneric<EntityType: BusinessObject
where EntityType : BusinessEntity, new() {
public BusinessObjectGeneric<EntityType() {
}
.....
causes error in constructor...
|
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...
|
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...
|
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,
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |