• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

create array of objects

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Lets make as if I have a class called Employer. How do I create an array of Employer.
I've tried the codes below but it doesn't work as I get "Employer@119c082" instead of Tim

Employer [] emp = new Employer[2]
emp[0] =new Employer("Tim");
System.out.println(emp[0]);

What is wrong with my codes?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like the Employer class isn't overridding the toString() method so you are getting the default implementation which just prints out the reference.

_M_
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------------------------------------------------
Employer [] emp = new Employer[2]
emp[0] =new Employer("Tim");
System.out.println(emp[0]);
----------------------------------------------------

Try using a for loop i.e

for(int i =0; i<emp.length; i++)
System.out.print(" "+emp[i]);
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abdi Duale:
System.out.print(" "+emp[i]);


Nope. That produces the same output -- calling Employee's toString method,
at the expensive of a needless -> "" + <-

OP: your problem has nothing to do with arrays: you should first try to
get output from a single, object:
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesnt the above code needs a constructor wih a String parameter:
public Employer(String name){
// do something
}

Regards
Balaji.S
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Balaji Sampath:
Doesnt the above code needs a constructor wih a String parameter:
public Employer(String name){
// do something
}



Yes.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephanie Ll:
Hi
How do I create an array of Employer.


You already know

Originally posted by Stephanie Ll:

Employer [] emp = new Employer[2]
emp[0] =new Employer("Tim");
System.out.println(emp[0]);

What is wrong with my codes?


Nothing is wrong with this code
You've created an array of Employers ("new") and assigned it to a variable that can contain such ("emp").
Then you've created an Employer object ("new") and assigned it to an element of the array.

That's all there is to it

Stuart
 
reply
    Bookmark Topic Watch Topic
  • New Topic