A simple question about the Date class

Category: Java SE
 
zjb5879
2009-08-02 02:44:57

Sponsored Links
Compile time is always a function of the error in getHireDay, neighborhoods

import java.util.GregorianCalendar;
import java.util.Date;
/ **
*
* @ author Administrator
* /
public class _Employee {
/ ** Creates a new instance of _Employee * /
; public _Employee (String name, double salary, int year, int month, int day)
{
_name = name;
_salary = salary;
Date _hireDay = (new GregorianCalendar (year, month-1, day)). getTime ();
}

/ / propertises
public String getName () {return _name;}
public double getSalary () {return _salary; }
public void getHireDay ()
{
System.out.println (_hireDay.toString ());
}

/ / fields
private String _name;
private double _salary;
private Date _hireDay;

}

Sponsored Links

mldu303237597
2009-08-02 03:03:53
compiler error? Or runtime error?
in your constructor:
Date _hireDay = (new GregorianCalendar (year, month-1, day)). getTime ();
phrase should be removed Date right?
mtmmtm
2009-08-02 03:22:00
Upstairs two correct.
because you are in another constructor in the _hireDate defines a local variable, so that it covers the class instance variables _hireDate, that is, in the constructor does not initialize class instance variables _hireDate, so when When you call getHireDate, throws null pointer exception.
li19870728
2009-08-02 03:41:57
Date _hireDay = (new GregorianCalendar (year, month-1, day)). getTime ();

already defined, no Date of
myway0126
2009-08-02 03:47:07
do not remove the Data, will appear with the same name hidden Le, class members that _hireDay is a null pointer, of course, there are exceptions.
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.