| Forums.Sureshkumar.net : A Perfect Place to Share Knowledge Blogs Games Magazines |
|
|
#1 (permalink) |
|
Member
Join Date: Jan 2007
Age: 24
Posts: 31
Thanks: 23 Thanked 0 Times in 0 Posts Thanks: 23
Thanked 0 Times in 0 Posts
Rep Power: 2
|
Hi friends ...
I am new to java so pls clear my doubt How many objects are created in the following piece of code? Myclasses class1, class2, class3; c1 = new Myclasses (); c3 = new Myclasses (); Thanks Suganya |
|
|
|
|
|
#2 (permalink) |
|
Member
Join Date: Jul 2006
Posts: 67
Thanks: 11 Thanked 43 Times in 29 Posts Thanks: 11
Thanked 43 Times in 29 Posts
Rep Power: 8
|
Re: Doubt in Object creation
Hi Suganya,
Only 2 objects are created, class1 and class3. The reference c2 is only declared and not initialized. So only two objects are created
__________________
Regards Surekha
|
|
|
|
| The Following User Says Thank You to surekhav For This Useful Post: |
suganyak (07-02-07)
|
|
|
#3 (permalink) |
|
Member
Join Date: Aug 2006
Location: Parli-V
Age: 24
Posts: 77
Thanks: 24 Thanked 9 Times in 8 Posts Thanks: 24
Thanked 9 Times in 8 Posts
Rep Power: 5
|
Re: Doubt in Object creation
Whenever you define int a,b,c; so we call a,b,c as variable this is datatypes it's mangae some memory on heap but initially it's define in heap as default value.that like as in java whenever you not use new keywords objects are not created means not get memory.we can only define reference variable.
In your ex Myclasses class1, class2, class3; c1 = new Myclasses (); c3 = new Myclasses (); here you create three reference variable class1, class2, class3; which refer to MyClasses and create only two objects i.e. c1 = new Myclasses (); c3 = new Myclasses (); i hope you understand.
__________________
Thanks & Regards Avadhoot S Dharmadhikari Hyderabad |
|
|
|
| The Following User Says Thank You to avadhoot.dharmadhikari For This Useful Post: |
t_mohan (08-02-07)
|
|
|
#4 (permalink) |
|
Junior Member
Join Date: Dec 2006
Posts: 20
Thanks: 0 Thanked 2 Times in 2 Posts Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 2
|
Re: Doubt in Object creation
hi Friends
Object creation has three phases 1.declaration 2.instantiation 3.intialization. Myclasses class1, class2, class3; this is declaration here we are reffering Myclasses objects with class1,class2 ans class3. c1 = new Myclasses (); c3 = new Myclasses (); The new instantiate the object means it allocates some memory to Myclasses Object. After that Myclasses constructer intaia;izes that object. so, only two objects created there. Thanks & regards, SureshKumar |
|
|
|
| The Following User Says Thank You to sureshkumar.t For This Useful Post: |
t_mohan (12-02-07)
|
|
|
#5 (permalink) |
|
Junior Member
Join Date: Feb 2007
Age: 26
Posts: 21
Thanks: 0 Thanked 0 Times in 0 Posts Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 2
|
Re: Doubt in Object creation
Hi Suganya,
Myclasses class1, class2, class3; When the above line of code is executed, the Java Virtual Machine(JVM) will create three reference variables class1, class2 and class3, but the objects are not created at this point i.e., the memory for the objects of type Myclasses is not allocated in JVM's memory. c1 = new Myclasses (); c3 = new Myclasses (); When the above two lines are executed, two objects of type Myclasses are created in the JVM's memory. Regards, Kris |
|
|
|
|
|
#6 (permalink) |
|
Member
Join Date: Aug 2006
Age: 25
Posts: 90
Thanks: 1 Thanked 10 Times in 7 Posts Thanks: 1
Thanked 10 Times in 7 Posts
Rep Power: 3
|
Re: Doubt in Object creation
Hi Suganyak,
Fine, The code you are given as follows: Myclasses class1, class2, class3; c1 = new Myclasses (); c3 = new Myclasses (); I dont know you either may or maynot observing the one point here. What is that is, there is discrimination between the Reference variables and the objects. From the abow code, class1, class2, class3 are the reference variables, then if you want to create the objects such as c1 and c3 you must given the code as follows: Myclasses c1 = new Myclasses (); Myclasses c3 = new Myclasses (); otherwise the java compiler(javac) rises an error,suchas c1 and c3 are the undefined variables in Myclasses. so, Finally there is no question of creating the objects in your given code. Thank you, If i commited any mistake. please correct me. |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| QTP 8.2 Tips & Tricks | slinkanand | Testing Tools & QA | 12 | 13-11-08 05:08 PM |
| Some QTP Faqs, Useful........ | kiran2710 | Testing Tools & QA | 4 | 24-10-08 01:18 AM |
| java JSP | naga | JAVA Technologies | 4 | 21-08-06 05:03 PM |
| Whats the difference between thesr | shilpa | VB / ASP.NET Technologies | 5 | 08-03-06 12:09 PM |
| JAVA collection | naga | JAVA Technologies | 0 | 21-02-06 06:01 AM |