Instantiating an Array of Custom Class
|
Author |
Message |
Slushba132
BustyLoli-Chan
Posts: 3,125.3993 Threads: 508
Joined: 20th Feb 2008
Reputation: -8.27558
E-Pigs: 73.1299
|
Instantiating an Array of Custom Class
So today I learned that when you instantiate an array of a custom class (in C# and Java)
you actually create an array of null references to this class. The class doesn't automatically initialize. And personally, I think THAT IS SO DAMN DUMB...
WHO THE FUCK NEEDS AN ARRAY OF NULL REFERENCES
WHY DON'T THEY JUST AUTO INSTANTIATE?
When I found out Java did this I almost slapped a bitch, but then I tested it in C# an the exact same thing happens...
I guess I've always taken the ArrayList for granted since it actually automatically instantiates your classes for you. Unfortunately, the ArrayList is not allowed on this assignment.
|
|
10/09/2012 04:09 PM |
|
ZiNgA BuRgA
Smart Alternative
Posts: 17,023.4213 Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.0333
|
RE: Instantiating an Array of Custom Class
Your statement merely dynamically allocates an array of size 4 as you said.
There are many cases where you may want an array with no actual instantiated objects, at the time of declaration. Simple example could be if you want to supply different constructor arguments for each object.
Perhaps the language could have a means of instantiating multiple objects automatically (if it doesn't already)... Although the following isn't too bad
(unsure about syntax since I don't really do C#)
(This post was last modified: 10/09/2012 04:59 PM by ZiNgA BuRgA.)
|
|
10/09/2012 04:52 PM |
|
Assassinator
...
Posts: 6,646.6190 Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
|
RE: Instantiating an Array of Custom Class
(10/09/2012 04:09 PM)Slushba132 Wrote: So today I learned that when you instantiate an array of a custom class (in C# and Java)
you actually create an array of null references to this class. The class doesn't automatically initialize. And personally, I think THAT IS SO DAMN DUMB...
WHO THE FUCK NEEDS AN ARRAY OF NULL REFERENCES
WHY DON'T THEY JUST AUTO INSTANTIATE?
Stop bitching, it's not like it's hard to do it yourself.
Also, what if your class can't initiate without arguments? Wouldn't work man.
|
|
10/09/2012 05:20 PM |
|
Slushba132
BustyLoli-Chan
Posts: 3,125.3993 Threads: 508
Joined: 20th Feb 2008
Reputation: -8.27558
E-Pigs: 73.1299
|
RE: Instantiating an Array of Custom Class
Most Classes have a default constructor...
On top of that you could implement ArrayList like support of
|
|
11/09/2012 06:29 AM |
|
Assassinator
...
Posts: 6,646.6190 Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
|
RE: Instantiating an Array of Custom Class
(11/09/2012 06:29 AM)Slushba132 Wrote: On top of that you could implement ArrayList like support of
The whole point of constructors that take arguments is that the arguments vary. So passing the same arguments to all the constructors like that is pointless.
(This post was last modified: 11/09/2012 07:01 AM by Assassinator.)
|
|
11/09/2012 07:01 AM |
|
ZiNgA BuRgA
Smart Alternative
Posts: 17,023.4213 Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.0333
|
RE: Instantiating an Array of Custom Class
(11/09/2012 07:01 AM)Assassinator Wrote: The whole point of constructors that take arguments is that the arguments vary. So passing the same arguments to all the constructors like that is pointless.
Not necessarily, and constructors don't necessarily have to need arguments. Really depends on the situation.
|
|
11/09/2012 05:46 PM |
|
Assassinator
...
Posts: 6,646.6190 Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
|
RE: Instantiating an Array of Custom Class
(11/09/2012 05:46 PM)ZiNgA BuRgA Wrote: (11/09/2012 07:01 AM)Assassinator Wrote: The whole point of constructors that take arguments is that the arguments vary. So passing the same arguments to all the constructors like that is pointless.
Not necessarily, and constructors don't necessarily have to need arguments. Really depends on the situation.
Lol, I know they don't necessarily need arguments, but there are cases that they do. What I'm saying is that if you hit such a case, then....
customClass[] customClasses = new CustomClass[4];
... is going to fail, and...
customClass[] customClasses = new CustomClass[4](CREATION ARGUMENTS);
... is pointless because passing the same arguments every time defeats the purpose.
(This post was last modified: 11/09/2012 06:58 PM by Assassinator.)
|
|
11/09/2012 06:56 PM |
|
ZiNgA BuRgA
Smart Alternative
Posts: 17,023.4213 Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.0333
|
RE: Instantiating an Array of Custom Class
(11/09/2012 06:56 PM)Assassinator Wrote: Lol, I know they don't necessarily need arguments, but there are cases that they do. What I'm saying is that if you hit such a case, then....
customClass[] customClasses = new CustomClass[4];
... is going to fail
Hardly an issue for the language - compilers can easily check for this.
(11/09/2012 06:56 PM)Assassinator Wrote: , and...
customClass[] customClasses = new CustomClass[4](CREATION ARGUMENTS);
... is pointless because passing the same arguments every time defeats the purpose.
In which case you wouldn't use that construct.
This doesn't prevent one from instantiating in a traditional fashion.
Not auto-instantiating is a sensible default to have, but having an additional operator, for instance, for doing this automatically is harmless IMO.
|
|
12/09/2012 12:57 AM |
|
Assassinator
...
Posts: 6,646.6190 Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
|
RE: Instantiating an Array of Custom Class
(12/09/2012 12:57 AM)ZiNgA BuRgA Wrote: Hardly an issue for the language - compilers can easily check for this.
But but but it's inconsistent!!!
I guess I just don't like inconsistent things.
|
|
12/09/2012 06:51 PM |
|
User(s) browsing this thread: