Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-27-2007, 04:08 PM
Member
 
Join Date: Dec 2007
Posts: 2
sergm is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-27-2007, 05:07 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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:

Code:
Employee[] staff = new Employee[3];
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-27-2007, 05:10 PM
Member
 
Join Date: Dec 2007
Posts: 2
sergm is on a distinguished road
Great! Thanks!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an array of nonprimitive objects Java Tip java.lang 0 04-14-2008 09:46 PM
HashMap with objects otoro_java New To Java 2 01-28-2008 04:28 PM
JSP implicit objects Java Tip Java Tips 0 12-26-2007 11:12 AM
Help with Objects! Shorinhio New To Java 1 07-10-2007 10:32 PM
Question mark colon operator question orchid Advanced Java 3 04-30-2007 11:37 PM


All times are GMT +3. The time now is 08:15 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org