Post Reply 
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.1259
Offline
Post: #1
Instantiating an Array of Custom Class
So today I learned that when you instantiate an array of a custom class (in C# and Java)

Code:
customClass[] customClasses = new CustomClass[4];


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
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA
Smart Alternative

Posts: 17,013.4068
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 445.4421
Offline
Post: #2
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

Code:
customClasses.Select(x => new CustomClass);

(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
Visit this user's website Find all posts by this user Quote this message in a reply
Assassinator
...

Posts: 6,638.3210
Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
Offline
Post: #3
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)

Code:
customClass[] customClasses = new CustomClass[4];


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
Find all posts by this user Quote this message in a reply
Slushba132
BustyLoli-Chan

Posts: 3,125.3993
Threads: 508
Joined: 20th Feb 2008
Reputation: -8.27558
E-Pigs: 73.1259
Offline
Post: #4
RE: Instantiating an Array of Custom Class
Most Classes have a default constructor...
On top of that you could implement ArrayList like support of

Code:
customClass[] customClasses = new CustomClass[4](CREATION ARGUMENTS);


11/09/2012 06:29 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Assassinator
...

Posts: 6,638.3210
Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
Offline
Post: #5
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

Code:
customClass[] customClasses = new CustomClass[4](CREATION ARGUMENTS);


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
Find all posts by this user Quote this message in a reply
ProperBritish
Daddy Proper
Team DreamArts

Posts: 5,665.1930
Threads: 192
Joined: 19th Nov 2008
Reputation: -2.36574
E-Pigs: 147.7035
Offline
Post: #6
RE: Instantiating an Array of Custom Class
because when you create an array you may not necessarily need to allocate memory for every instance that will be in that array straight away. If you don't need what's in that array straight away, why are you instantiating it? They'll just sit there in memory being useless until you need them, taking up space.

[Image: rsz_contrast.png]

Spoiler for More sigs:
[Image: 6xu74t8]
[Image: sig.php]

[Image: 656embk]
[Image: sig.png]
11/09/2012 02:02 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA
Smart Alternative

Posts: 17,013.4068
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 445.4421
Offline
Post: #7
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
Visit this user's website Find all posts by this user Quote this message in a reply
Assassinator
...

Posts: 6,638.3210
Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
Offline
Post: #8
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
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA
Smart Alternative

Posts: 17,013.4068
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 445.4421
Offline
Post: #9
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
Visit this user's website Find all posts by this user Quote this message in a reply
Assassinator
...

Posts: 6,638.3210
Threads: 176
Joined: 24th Apr 2007
Reputation: 8.53695
E-Pigs: 140.8363
Offline
Post: #10
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
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

 Quick Theme: