![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Class inside another class
just want to know if i'm crazzy or if this actually does work
import java.awt.*;
import hsa.Console;
import java.util.Calendar;
class CalendarTime
{
public long milli;
public long minute;
public long rightNow;
public long rightThen;
//public Calendar() = Now;
// contructor
public CalendarTime ()
{
Calendar Now = Calendar.getInstance ();
this.milli = Now.getTimeInMillis (); // contructs current time
this.minute = this.milli / 60000;
this.rightNow = this.minute;
this.rightThen = this.minute + 10; //default is 10 minutes.
}
// used to set a different time limit
public void setTime (int x)
{
this.rightThen = ( Now.getTimeInMillis () / 60000 + x);
}
public int getTimeLeft ()
{
return this.rightThen - this.rightNow; // time left
}
}
CalendarTime expiryDate = new CalendarTime ();
c.print (expiryDate.getTimeLeft ());So am i crazzy or does this actually work? I'm making a "Calendar" object in the contructor from the Calendar class in my own class. The error that I get occurs in the blue writting. I can't use "Now." so what do i use? If it does work can you help me out please? Turns out i'm learning quite a bit tonight :banana: (up to this point anyways).
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 884
Rep Power: 4
![]() |
At the moment you are making the "Now" variable local to your constructor. You were on the right track when you made it a member variable.
class CalendarTime
{
public long milli;
public long minute;
public long rightNow;
public long rightThen;
public Calendar Now;
// contructor
public CalendarTime ()
{
Now = Calendar.getInstance ();
this.milli = Now.getTimeInMillis (); // contructs current time
this.minute = this.milli / 60000;
this.rightNow = this.minute;
this.rightThen = this.minute + 10; //default is 10 minutes.
}
// used to set a different time limit
public void setTime (int x)
{
this.rightThen = ( Now.getTimeInMillis () / 60000 + x);
} |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
That's quite the gratuitous use of the keyword "this". You don't really need to use it in your case.
|
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|