Interclass Variables - Static/Non-static

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?

[2415 byte] By [CallMeRexa] at [2007-11-27 7:11:12]
# 1
Each instance of the Timer class will have it's own seconds count, so your if your other class is to get the count, it needs to know which instance of the Time class is relevant. So it needs a reference to the Timer class.
malcolmmca at 2007-7-12 19:02:48 > top of Java-index,Java Essentials,Java Programming...
# 2
Would you be able to elaborate on that point? I'm a newb.
CallMeRexa at 2007-7-12 19:02:48 > top of Java-index,Java Essentials,Java Programming...
# 3

Timer is a class. Until you create an instance of that class with new there's no actual nSeconds field. And when you do, you could create any number of instances, each with their own nSeconds field. In order for the code in the other class to know which object of class Timer to get the nSeconds from, it needs a reference to that instance, set directly or indirectly from the reference returned by the new

malcolmmca at 2007-7-12 19:02:48 > top of Java-index,Java Essentials,Java Programming...