Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static string

 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, can anybody throw light on this,when i declare the string as static it gives me compiler errs but when i remove it ,it works ?
public class spacestr{
public static void main(String args[])
{
static String s=" srinivas ";
//String s=" srinivas ";
System.out.println(s.charAt(2));
System.out.println(s.trim().charAt(2));
System.out.println(s);
}
}
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivas,
AFAIK, member variables cannot be declared as static. Member variables' scope is only inside the member block, while static variable is always considered to be a class variable. Here's what JLS says..
JLS 14.4.2
quote:
The scope of a local variable declaration in a block is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.
JLS 4.5.3
quote:
A class variable is a field declared using the keyword static within a class declaration, or with or without keyword static within an interface declaration. A class variable is created when its class or interface is prepared and is initialized to a default value. The class variable effectively ceases to exist when its class or interface is unloaded.
- eric
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables declared inside of a method can not be static.
------------------
Co-Moderator of the Programmer Certification Forums
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a member is declared static , it can be accessed before any objects of it's class are created , & without reference to any object .

What this means is that the static member can be used by itself , without reference to a specific instance . When u the declare the String as static in the main() method it refers to that particular instance of main() which it shouldn't (or can't) . To sort out your problem put your static statement before the main() .
This is also the reason why main() is declared static 'cause it needs to be called before any objects exist .
U'r thing made me post "== operator checks for object references" in the beginners area . Check it out .
It's 8 am & i still haven't slept . Java is just too stimulating . And in case what i wrote is weird then i probably need to sleep .
U would've got this 2 hrs earlier but my ISP sux !
[This message has been edited by Ashish Hareet (edited July 15, 2001).]
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Can't can I declare static veriables in Static method as well??
 
Detlev Beutner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a method (static or not), a variable declared there is not (can't be) static or a member variable - it is just a local variable. So Erics comment is quite right - referring to the JLS (which, of course, is talking about local variable). But it is /not/ a question if "member variables cannot be declared as static". Here, the string s isn't a "member variable" at all...
Hope it helps
Detlev
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic