GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Nov-2006, 00:59
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough

create array of my class


Hi all!


My question relates to creation of an instanse of a class.

When you create a instance of class the constructor gets a message telling it to create a object of that class. I hope so far so good! Ok to my real question, what happens when i create an array of my class? Shouldn't my constructor get calls equal to n.
i.e
n=6;
MyClass[] c = new MyClass[n];

The thing is that i tried to follow the creation of an array of my class but nothing happens, i have JOptionPane.showMessage(null, i++) that popups when i call the constructor. But nothing happens when i create an array but popups when i create one instanse.

In c++ creating an array of a class will message the constructor to create [x] x instances of the class.

How do i create an array of a my class in Java?

Thanks for any reply
Last edited by amad1337 : 10-Nov-2006 at 02:04.
  #2  
Old 10-Nov-2006, 06:12
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough

Re: create array of my class


Ok!

Let me redefine my question.

In c++ the statement:

Myclass *c = Myclass[n];

will call the default constructor n times, to initiate data members.

In Java i wrote this statement:

Myclass []c = new Myclass[n];

The default constructor doesn't get called. The array just get null references, some how i understand that array (c) get null reference but i would like to find equivalent statement like the one for c++ i wrote down in the first example for Java.

Please post any thought...
  #3  
Old 10-Nov-2006, 08:58
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 958
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: create array of my class


We're with you...

Unfortunately, Java doesn't handle the arrays quite the same, as you noticed.

The initial declaration that you did creates 6 MyClass references, and then a loop is necessary to create a new MyClass object for each element. [Ugh, I know] But of course, Java makes it easy to then set/create objects in a loop:
JAVA Code:
  for ( int index = 0; index < c.length; ++index )
  {
     c[index] = new MyClass();
  }
I haven't found any utility [yet] that helps do both in one step. I did find a fill() function in java.util.Arrays, but unfortunately it assigns the SAME object to the entire array if you try to do:
JAVA Code:
Arrays.fill( c, new MyClass() ) 
-- which isn't very helpful either. I haven't looked into the newer 1.5 API to see if there may be anything new to help perform 'one step' creations.

Maybe someone else may have another idea/trick.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #4  
Old 10-Nov-2006, 09:44
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough

Re: create array of my class


thx alot TurboPT. Thats all the medicine i needed

I was going through all documentation sun.java i was literally losing my mind. Now that i know that java doesn't support this machanism i can go on with my life

I will note the notation you made about how to create the array och class type... Thx again TurboPT
  #5  
Old 11-Nov-2006, 08:26
sitvasu sitvasu is offline
New Member
 
Join Date: Mar 2006
Posts: 10
sitvasu is on a distinguished road

Re: create array of my class


I think your problem is solved.But I want to make it more clear.

CPP / C++ / C Code:
class ex
{
     int i;
     public :
            ex()
            {
                   i=0;
                   cout<<"constructor invoked\n";
             }
};

int main()
{
      ex obj;
      return 0;
}

This code will print "constructor invoked" one time .
Here obj is an object.

But the same thing in java will not print that statement.
Because obj becomes object reference but not object.
Memory will be allocated only with a new operator ,
obj = new ex();//now constructor will be invoked.

So,the point is when you create an array of your class,it is an array of references in Java ( In C++ it is an array of instances of your class )

In java you have to create the instances of your class using 'new' which invokes the constructor.
Last edited by LuciWiz : 11-Nov-2006 at 13:00. Reason: Fixed [cpp] tag
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 06:06
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 03:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.