• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

static variables

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i want to know i can decalre a variable static within a function.Look at this code
String createPacketData()
{
static int seqnumber=1;
String data="Content Number";
String result=data+"="+seqnumber;
return (result);
}
Iam geting many errors when i try to declare a varible static within the Function
HERE IT IS:
illegal start of expression
static int seqnumber=1;
^
./CliSender.java:31: cannot resolve symbol
symbol : variable seqnumber
location: class CliSender
String result=data+"="+seqnumber;
please let meknow if it is illegal
thanks
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot declare a variable in any functional blocks as the static variables are class variables.Declare outside the any methods and use them as required.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want to declare a static variable inside a method?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All variables declared inside methods are local to the method. That means that the value of the variable is stored in the stack instead of in an object on the heap (like a member variable) or in memory related to the classfile (like a static variable).
When the method ends - the stack is disposed of, and the value of the variable is gone - poof!
A static variable must stick around forever to be available to any object that whats to use it.
If it must be static try it this way:

But I expect that it really does not need to be static. What are you trying to accomplish?
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like C programming, where you sometimes want to keep a counter inside a method, doesn't it ?
W.
 
We don't have time for this. We've gotta save the moon! Or check this out:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic