Forums Register Login

Fun with Constructors, etc

+Pie Number of slices to send: Send
Happy Holidays, All!
Okay - what am I doing wrong here:
I have two classes, Employee1 and EmployeeMaker:
======================================================
public class Employee1
{
private String firstName;
private String lastName;
private int empID;
private float salary;
//get and set for firstName
public String getFirstName ()
{
return firstName;
}
public void setFirstName (String firstNameIn)
{
firstName = firstNameIn;
}

//get and set for lastName
public String getLastName ()
{
return lastName;
}
public void setLastName (String lastNameIn)
{
lastName = lastNameIn;
}
//get and set for empID
public int getEmpID ()
{
return empID;
}
public void setEmpID (int empIDIn)
{
empID = empIDIn;
}
// get and set for salary
public float getHourlyWage ()
{
return salary;
}
public void setSalary (float wageIn)
{
salary = wageIn;
}
public Employee1 ()
{
firstName = "Unknown";
lastName = "Unknown";
empID = 0;
salary = 0.00f;
}
public Employee1 (String firstNameIn, String lastNameIn, int empIDIn, float salaryIn)
{
firstName = firstNameIn;
lastName = lastNameIn;
empID = empIDIn;
salary = salaryIn;
}
} //end of class
========================================================
public class EmployeeMaker
{
Employee1 employee101 = new Employee1 ("Kim", "Yee", 101, 40000.00f);
Employee1 employee102 = new Employee1 ("John", "Reynolds", 102, 55000.00f);
Employee1 employee103 = new Employee1 ("Elena", "Gonzales", 103, 50500.00f);
Employee1 employee104 = new Employee1 ("Jim", "OShea", 104, 75000.00f);
System.out.println (employee101.getLastName());
}
===========================================================
Why will that last line not compile? What's wrong with
System.out.println (employee101.getLastName());
???
Someone enlighten me please!
EB
+Pie Number of slices to send: Send
I'm not sure; on the surface, it looks like it should work. What, exactly, is the error message you are getting, and is your code the same as what is posted here? (Sometimes you make a typo in your code that you don't make when posting code here -- that's why I recommend cutting-and-pasting code, and remember to put it between [CODE] and [/CODE] tags!)
+Pie Number of slices to send: Send
Why will that last line not compile?
It's not part of any method.
If you're intending to launch this program like this:
java EmployeeMaker
then you can put your System.out.println( ... ) inside a "main" method like this:
public static void main( String[] args )
{
System.out.println (employee101.getLastName());
}
As to the
Employee1 employee101 =
Employee1 employee102 =
...
you can either (1) declare them static, so a static method such as main can access them, or (2) also put them in main.
+Pie Number of slices to send: Send
Talk about a big fat DUH on that one....
Good Grief...look right past the obvious on that one! No main()!!!
It's obviously time for bed.
nite nite.
CB
Willie Smits can speak 40 languages. This tiny ad can speak only one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 803 times.
Similar Threads
TreeSet Question
Problem with refresh() method of EntityManamger in JPA
How to set default values in an spring form input?
Populate Complex Java Object using Reflection
private constructor
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:29:01.