• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static method local variables

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

the following declaration for i fails compilation;

public static void staticFunc()
{
static int i = 2; // this line here.
}

Temp.java: illegal start of expression
static int i = 2;
^
1 error

why are method local variables NOT allowed to be declared static?

thanks much.

G
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java doesn't allow you create a static variable inside a method.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static is an identifier only meant for top-level class variables, methods, and nested classes.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variable --> Class

Local variable ---> Starts its life inside method and is also destroyed when the method is completed. They are always stored on stack not heap.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GrahmO,

Only modifier allowed to local variables is the 'final' modifier. Thus, the modifier 'static' is against the fair play..!

The purpose of the static variable is to allow the all the instances to share a single variable, and to get the best out of it, modify it as frequently as possible.

Remember, all the method local variables are destroyed as the method returns. Imagine a variable of 'static' context is allowed.

Is there a purpose of keeping such a class variable (defined in a method), as all the localized variables are dead and buried once the method has returned..?

Hope that explains mate..!

Regards,
Priyanka.
[ March 08, 2006: Message edited by: Priyanka Kolamba Patabendige ]
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Concerning Ashley's post it's not exactly correct.

You can declare local variables that are created on heap. I'm refering to reference variables declared inside a method.

Only Local variables for primitives are created on stack.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I'm studying for scjp 1.4 and I don't understand what 'static' access modifier is (for methods and variables). I know it's a silly question, but I've already read the K&B chapter many times and this subject still difficult for me. Someone could help me?? Some easy examples?? Please ... this is important for me ....

Regards,
Marcella Spiropulos
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marcella,

Firstly, keep in mind that static members and instance members have opposite meanings.

Instance members :
Instance members belong to a particular instance of a Class.

So, if you have for example, an instance variable named as age, every time you create an instance by using new keyword, you will have a different and distinct age variable belonging to each instance created.

In this case, changes made in one instance specific DOESN'T affect the other instances already existing.

Static members :

Totally opposite from preceding explanation.

Static members belong to a class and NOT to a instance. In other words, a static member is shared through all instances for that class.

For example, supposing you have a class named as CarRent that contains informations about each car that was rented to a few customers.

Now, you want to know how many cars are rent at this moment, so, the best way to programatically represent this scenario is by using a static variable so that, every time your program creates a new rented car by using new CarRent() the class must add by one the static variable called totalCarRent.

So, every instance will share this variable and changes made by one instance will be reflected in another instance as well.

Hope that helps.
 
Marcella Spiropulos
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edisandro Bessa !

Thank you so much ... now I understand about 'static' modifier !!!

Regards,
Marcella Spiropulos
 
reply
    Bookmark Topic Watch Topic
  • New Topic