This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
RE:Query
Posted by Kishori Sharan on July 22, 2000 at 10:03 PM
Hi What do you want to achieve declaring a varialbe as static within a method? You cannot do that. You can declare the memeber variable of a class as static not the local variable of a method as static. Whatever variables you define within a method are local to that method. They are created only when method is invoked and destroyed when that method returns. So , you can declare only class's member variables as static not local variable of a method. Here it doesn't matter whether your method is static or non-static.Some more differences between them follows. class C1 { // It is member variable and can be static or non-static int memeber_variable1 ; static int member_variable2 ; public void myMethod ( ){ // They are local variable for this method, so they // cannot be declared as static int local_var1 = 122; // You must initialize your // local variable to your method // otherwise compiler will //complain if you try to use the // method's local variable later. // However,class memeber // variables are initialized // by java ( all numerics to // ( zero ) int local_var2 = 133 ;
}
}
Thanx Kishori
Replies:
|