Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interclass Variables - Static/Non-static

807605Jun 12 2007 — edited Jun 12 2007
Hello. I'm trying to modify a pre-existing class to pass a variable (in this case, a number of seconds) to another class in the same package. However, apparently I'm referencing a non static variable from a static context. I've browsed thru pages in previous forums but nothing quite seems to help me with this problem.

<code>
import java.util.Date;

public class Timer
{
private Date start_;



/**
* Start timer.
*/
public Timer()
{
reset();
}

public long nSeconds;

/**
* Returns exact number of milliseconds since timer was started.
*
* @return Number of milliseconds since timer was started.
*/
public long getTime()
{
Date now = new Date();
long nMillis = now.getTime() - start_.getTime();

return nMillis;
}



/**
* Restarts the timer.
*/
public void reset()
{
start_ = new Date(); // now
}



/**
* Returns a formatted string showing the elaspsed time
* suince the instance was created.
*
* @return Formatted time string.
*/
public String toString()
{
long nMillis = getTime();

long nHours = nMillis / 1000 / 60 / 60;
nMillis -= nHours * 1000 * 60 * 60;

long nMinutes = nMillis / 1000 / 60;
nMillis -= nMinutes * 1000 * 60;

long nSeconds = nMillis / 1000;
nMillis -= nSeconds * 1000;

StringBuffer time = new StringBuffer();
if (nHours > 0) time.append (nHours + ":");
if (nHours > 0 && nMinutes < 10) time.append ("0");
time.append (nMinutes + ":");
if (nSeconds < 10) time.append ("0");
time.append (nSeconds);
time.append (".");
if (nMillis < 100) time.append ("0");
if (nMillis < 10) time.append ("0");
time.append (nMillis);

return time.toString();
}

public static long nSecs = nSeconds; // <----my issue is here

/**
* Testing this class.
*
* @param args Not used.
*/
public static void main (String[] args)
{
Timer timer = new Timer();

for (int i = 0; i < 100000000; i++) {
double b = 998.43678;
double c = Math.sqrt (b);
}

System.out.println (timer);
}
}

</code>

Thoughts?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 10 2007
Added on Jun 12 2007
3 comments
180 views