This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why static variables and not allowed inside a method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why static variables and not allowed inside a method ?" Watch "Why static variables and not allowed inside a method ?" New topic
Author

Why static variables and not allowed inside a method ?

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Why static variables and not allowed inside a method ?

Tried declaring static variables inside a method but it isint allowing

It is said the memory refernce is same - when we say a varible as static,

means it maintains it as constant

code snippet

void method()
{
static int i = 0;// is not working
}



static void method()
{
//does it mean that any variables declared here, would be static
}

your help is appreciated .


Discussion - the powerfull way to excellence!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
More of a beginner's question.

Static variables belong to the class; variables declared inside a method are local variables and belong to that method. So you can call them final, but not static or public or protected or private.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

this ll help


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Campbell Ritchie:
More of a beginner's question.

Static variables belong to the class; variables declared inside a method are local variables and belong to that method.

Hi campbell,

I know that,

#static variables are class variables.
#They are accessed only by the class and not by the class Instances.
#static variables are accessible from static methods and className.

But c++, allows declaration of static variables inside a method.

Why that feature of "static" has been removed in java ?Is my question?


So you can call them final, but not static or public or protected or private.


I agree !
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Java is NOT C++.
Never has been, I would suggest if you want to use a feature of C++ you should chose to use C++ and not Java, you will be much happier.

However, my answer is, I suspect that static variables have a slightly different meaning in C++ to what they do in Java.

I actual cant see the need to have Java static variables in a method, the only use I can see is to count the number of times a method is called (cant think of a real world use), for this you can use an instance variable, and if you wanted that count for all objects of a given class use a static (instance variable).

[ June 19, 2008: Message edited by: Gavin Tranter ]
[ June 19, 2008: Message edited by: Gavin Tranter ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
Originally posted by Gavin Tranter:
Java is NOT C++ . . . I suspect that static variables have a slightly different meaning in C++ to what they do in Java.
There are actually several C/C++ keywords which have different meanings from Java. They include char, static and protected. I think a lot of poeple have got very confused by the similar keywords, not realising the differences.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
. . . and the meaning of "static" is very different in Java and in C.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Thanks for the clairification Campbell, I suspect that it was very different.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Honestly C (yes, this is a C feature, not a C++ feature) static local variables are darn handy, and I've wished for them more than once in Java. Basically they're like member variables of a function: they get initialized the first time the function is called, and persist while the application runs.

The Java equivalent is to create an object, perhaps an inner class, to hold the methods and the variables -- basically if you find yourself wanting this, then you probably have a class that needs to be broken into multiple smaller classes.


[Jess in Action][AskingGoodQuestions]
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
The use of a static variable in a method/function causes it to be non-reentrant. This can cause no end of headaches. It's a language design choice to not allow you this rope to hang yourself with - much of Java is designed that way.

You can still define a private static variable in the class and use it only in one method, but it's more explicit that the variable you are defining exists beyond an invocation of the method.

It's not an limitation of functionality, only one of syntax.

I think it's a good decision given the frequency of multithreaded programs (almost anything with a UI or communications outside the program), and that will only increase as multi-core platforms become more prolific.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Gavin Tranter:
Java is NOT C++.
Never has been, I would suggest if you want to use a feature of C++ you should chose to use C++ and not Java, you will be much happier.

However, my answer is, I suspect that static variables have a slightly different meaning in C++ to what they do in Java.

I actual cant see the need to have Java static variables in a method, the only use I can see is to count the number of times a method is called (cant think of a real world use), for this you can use an instance variable, and if you wanted that count for all objects of a given class use a static (instance variable).

[ June 19, 2008: Message edited by: Gavin Tranter ]

[ June 19, 2008: Message edited by: Gavin Tranter ]



Hi Gavin Tranter

Find the reasoning of Ernest Friedman-Hill

that was really good , its for the same reason am looking for.

accidently found this when i tried declaring in the code.

where i really need it as per my official design of the system.,

For this simple lame reason that static is not supported with java inside a method, now i cannot shift my whole official design system to c++.

We will keep this as a discussion , rather choosing that to be c++ or java.

Any how, your response was good.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Bill Shirley:
The use of a static variable in a method/function causes it to be non-reentrant. This can cause no end of headaches. It's a language design choice to not allow you this rope to hang yourself with - much of Java is designed that way.

You can still define a private static variable in the class and use it only in one method, but it's more explicit that the variable you are defining exists beyond an invocation of the method.

It's not an limitation of functionality, only one of syntax.

I think it's a good decision given the frequency of multithreaded programs (almost anything with a UI or communications outside the program), and that will only increase as multi-core platforms become more prolific.



"I think it's a good decision given the frequency of multithreaded programs (almost anything with a UI or communications outside the program), and that will only increase as multi-core platforms become more prolific"

As far this argument is concerned, its clear that there are some advantages and some dis advantages of using it, using as - inside (c++) / out (java).

But some how i carry on with what ernest said.

"It's not an limitation of functionality, only one of syntax."

thanks, i thought it was limitation, but it has several major advantages too.

Thanks for posting
[ June 19, 2008: Message edited by: ram kumar ]
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Ernest Friedman-Hill:
Honestly C (yes, this is a C feature, not a C++ feature) static local variables are darn handy, and I've wished for them more than once in Java. Basically they're like member variables of a function: they get initialized the first time the function is called, and persist while the application runs.

The Java equivalent is to create an object, perhaps an inner class, to hold the methods and the variables -- basically if you find yourself wanting this, then you probably have a class that needs to be broken into multiple smaller classes.


This is excellent ! thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why static variables and not allowed inside a method ?
 
Similar Threads
static method local variables
static members in inner classes
why compile error??????/
static variables in beans
Static variables in Static method