Creating an Array of Class Objects
From Java Example Source Code
Contents |
[edit] Overview - Creating an Array of Class Objects
This is a Java example program.
[edit] Java Source Code
- Package: example.array
- File: EmployeeTest.java
package example.array;
public class EmployeeTest {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry Hacker", 3500);
staff[1] = new Employee("Carl Cracker", 7500);
staff[2] = new Employee("Tony Tester", 3800);
for (int i = 0; i < 3; i++)
staff[i].print();
}}class Employee {
private String name;
private double salary;
public Employee(String n, double s) {
name = n;salary = s;}public void print() {
System.out.println(name + " " + salary);
}}
[edit] What Result You Can Get
Run the program, you will get:
Harry Hacker 3500.0 Carl Cracker 7500.0 Tony Tester 3800.0
[edit] Required External Libraries and/or Files for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
