I have the following code:
package application;
import java.util.Timer;
import java.util.TimerTask;
public class Application {
public String name = "Brad" ;
public static void main ( String [ ] args ) {
Timer time = new Timer ( );
time.schedule ( new TimerTask ( ) {
@Override
public void run ( ) {
System.out.println( "Name: " ) ;
}
}, 0, 10000 );
}
}
how can I access methods and / or properties of the class within run () function?
honestly, I do not know how I can have access to the context .. and no idea how to pass the object as parameter, as in PHP
You can access instance variables but in your case you can not access
namesince it is notstatic. If you want to use field variables, then you need to declare them asfinalin order to be able to use them inside an anonymous class.Example: