• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Fun with Constructors, etc

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!)
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Elaine Banks
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I will suppress my every urge. But not this shameless plug:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic