• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static method can only handle static variables but why is it still working in case of local variable  RSS feed

 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



year isnt declared static yet it is running successfully why is that so??
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles because year is a local variable, not a non-static class member...
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean to say it is not cumpolsary for the local variables of static method to be static
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:you mean to say it is not cumpolsary for the local variables of static method to be static


They cannot even be static. Java doesn't support that.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why aint local variables in static method static and why does it produce error when we make its local variable static
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:why aint local variables in static method static and why does it produce error when we make its local variable static


Dude, do you know what static means in java? It means that the field or method belongs to the class and not the instance, thus it can exist without an instance and it is shared by all instances! A local variable lives inside the scope of the method (or a smaller, enclosing scope, e.g. an if block), practically it "belongs to the method", not to the instance or class!
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i do know what does static mean thnksss for reminding me tht local variables are within the method scope thats why it cant belong to class hence not static
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:why aint local variables in static method static and why does it produce error when we make its local variable static


It's for the reason I said. Java doesn't support static variables in methods. There has been an intense discussion whether Java should support them but those who suggest you can as well do this instead have won (so far).

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!