| Author |
static variable inside function definition
|
Juggy Obhi
Ranch Hand
Joined: Jul 02, 2007
Posts: 51
|
|
|
Is this possible to declare/define a static variable inside a function member???
|
It does not matter how many times you fall,what matters is how many times you stand back.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
Welcome to the Ranch. Inside a method-no. Inside an inner class, probably yes.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Juggy Obhi
Ranch Hand
Joined: Jul 02, 2007
Posts: 51
|
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
|
Please leave the post to be archived; we occasionally look at postings several years old.
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2549
|
|
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
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
 |
|
|
subject: static variable inside function definition
|
|
|