Results 1 to 3 of 3
  1. #1
    sergm is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default Creating objects question

    Hi All,

    I'm a Java beginner, so, don't be angry if I'm asking really stupid things.
    But please help me to understand the following:
    I have a simple Java program.



    import java.util.*;

    public class EmployeeTest
    {
    public static void main(String[] args)
    {
    // fill the staff array with three Employee objects
    Employee[] staff = new Employee[3];

    staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
    staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

    // raise everyone's salary by 5%
    for (Employee e : staff)
    e.raiseSalary(5);

    // print out information about all Employee objects
    for (Employee e : staff)
    System.out.println("name=" + e.getName()
    + ",salary=" + e.getSalary()
    + ",hireDay=" + e.getHireDay());

    }
    }

    class Employee
    {
    public Employee(String n, double s, int year, int month, int day)
    {
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    // GregorianCalendar uses 0 for January
    hireDay = calendar.getTime();
    }

    public String getName()
    {
    return name;
    }

    public double getSalary()
    {
    return salary;
    }

    public Date getHireDay()
    {
    return hireDay;
    }

    public void raiseSalary(double byPercent)
    {
    double raise = salary * byPercent / 100;
    salary += raise;
    }

    private String name;
    private double salary;
    private Date hireDay;
    }


    My question is about this part:
    for (Employee e : staff)
    System.out.println("name=" + e.getName()
    + ",salary=" + e.getSalary()
    + ",hireDay=" + e.getHireDay());


    How is it possible to create such an object Employee e and use it afterwards without instantiation? I mean without using the operator new?
    Please explain it to newbie :) thanks a lot in advance

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Java Code:
     for (Employee e : staff)
    This means 'for each employee in the array staff' right? Well to be able to do stuff to each of those employees, you need a way to identify them. So there is really no object being created. 'e' just represents each employee in the staff array that was created here:

    Java Code:
     Employee[] staff = new Employee[3];

  3. #3
    sergm is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  3. HashMap with objects
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 03:28 PM
  4. JSP implicit objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:12 AM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •