Today I suddenly realized that Java never specifies access level for local variables when I try to declare a variable for main method. I also tried to give it a access level modifier, but it doesn't compile. The error is: Illegal modifier for parameter t1; only final is permitted
package myThread;
import java.lang.Thread;
import java.lang.String;
import java.lang.System;
public class PrintThread extends Thread{ //extends Thread class
private long sleepTime;
public PrintThread(String name)
{
super(name);
this.sleepTime = (long) (java.lang.Math.random() * 5000);
System.out.println("Thread: " + getName() + ", sleepTime: " + sleepTime);
}
public void run() // override run method
{
System.out.println(getName() + " go to sleep...");
try
{
sleep(sleepTime);
} catch (InterruptedException e)
{
e.getMessage();
}
System.out.println(getName() + " out of sleep!");
}
public static void main(String[] args)
{
public PrintThread t1 = new PrintThread("T1"); // CANNOT COMPILE
t1.start(); // start thread by start() method
}
}
As we know, we need to use public protected package private static final etc to specify a member variable of java class.
But we never do this for class method (static or not) variable.
I was stuck with this conception all day. Previously, I just write java code as usual but didn't realize this phenomenon.
Could you give me hints. I CANNOT persuade myself just by eliminate public keyword for t1.