aspose file tools
The moose likes Beginning Java and the fly likes static variable inside function definition Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "static variable inside function definition" Watch "static variable inside function definition" New topic
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
    
    4
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
    
    3

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
    
    4
Please leave the post to be archived; we occasionally look at postings several years old.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2549
    
    2

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|
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: static variable inside function definition
 
Similar Threads
Strings information required
Static Main function VS Static user define function
static variable in method
static variable inside static function
Initializing final arguments