![]() |
|
#1
|
||||
|
||||
create array of my classHi 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
|
||||
|
||||
Re: create array of my classOk!
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
|
||||
|
||||
Re: create array of my classWe'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:
JAVA Code:
Maybe someone else may have another idea/trick. __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#4
|
||||
|
||||
Re: create array of my classthx 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
|
|||
|
|||
Re: create array of my classI think your problem is solved.But I want to make it more clear.
CPP / C++ / C Code:
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 GIDBlog
Toyota - 2008 September Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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