• Post Reply Bookmark Topic Watch Topic
  • New Topic

static variable inside function definition  RSS feed

 
Juggy Obhi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this possible to declare/define a static variable inside a function member???
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Inside a method-no.
Inside an inner class, probably yes.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way to find out is to write a small program and try to compile it.

In Java you cannot create static variables inside a method, like you can in C or C++ inside a function.

Also, you can't create a static member variable in a non-static inner class. You can only create a static member variable in a top-level class or in a static inner class.
 
Juggy Obhi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is for the first time i posted a doubt in javaranch...i wasn't aware people here could be as good and helpful...thank you all...and Jesper please tell me Why only STATIC INNER CLASSES?....and one more thing...should the post(like this one) be deleted when you get satisfied with the answers...or left alive for others???
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please leave the post to be archived; we occasionally look at postings several years old.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
static variables can be declared only in static class or static inner class because whatever is static is associated with the class itsel and not with the instances of the class.


Hope this helps
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!