• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Getting problem in declaring static variable.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why cant we declare as below..

public class StaticTest
{

public static void main(String a[])
{
static int j=11;
++j;
System.out.println("The value of J is "+j);
}
}

when im compiling its saying:
StaticTest.java:6: illegal start of expression
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class StaticTest
{
static int j=11;
public static void main(String a[])
{
++j;
System.out.println("The value of J is "+j);
}
}

Now it will work.
 
Rajani kanth Pandiri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply
I did this already.
Why cant i declare as i mentioned.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because local variables accept only "final" as modifier. Think about it, static variables belong to classes (not objects), but local variables belong to methods, their life-cycle ends when the method ends running, they are not visible outside that method.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
local variables couldnot have any access type except final.right!
now variables which are declared within method are called local variables.
now main() is also like a normal method. coz when give private main() it compiles. so variables within main are also local so couldnot have any
access type.
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The usage of static variable in a method voilates the contract of static. they are class variables which are initialized on class loading. they are not limited to any particular object and the value of static variables is shared among objects.
therefore it is illegal to declare staic variable(class variable) withing method(non-static method).

i hope this is clear to u
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the below is not true!!!

The usage of static variable in a method voilates the contract of static. they are class variables which are initialized on class loading.



but the below is correct

local variables could not have any access type except final



if it is final u cannot reassign the value ???

try using other specifiers(public, private....transient etc and see for results)
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the below is true!!!


quote:
--------------------------------------------------------------------------------
The usage of static variable in a method voilates the contract of static. they are class variables which are initialized on class loading.
--------------------------------------------------------------------------------

They are initialized(for the first time) during class loading only
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When Class definition is loaded all its static members get allocation of space and become available for use, When Method is declared as static, It is Available without the instance of Class, it was very basic, Now why local variables cant be static, possibly because with each static or non-static method execution, local vairables gets space allocation, Where as static means sharing across multiple instances, So can get an idea about why this restriction might have came, Continuing, If it is so why local variables can be final, possibily because here compiler can optimise few things as local final variable will only hold same values with each invocation i.e. Value when it is declared.
[ February 02, 2006: Message edited by: Vishal Angrish ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic