wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static string" Watch "static string" New topic
Author

static string

srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
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);
}
}


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
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

Joined: May 05, 2000
Posts: 13974
Variables declared inside of a method can not be static.
------------------
Co-Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
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

Joined: Oct 18, 2000
Posts: 201
But Can't can I declare static veriables in Static method as well??


Swati Kale
SCJP
SCWCD
Detlev Beutner
Ranch Hand

Joined: Jul 13, 2001
Posts: 76
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
 
GeeCON Prague 2014
 
subject: static string