Forums Register Login

static string

+Pie Number of slices to send: Send
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);
}
}
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Variables declared inside of a method can not be static.
------------------
Co-Moderator of the Programmer Certification Forums
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
But Can't can I declare static veriables in Static method as well??
+Pie Number of slices to send: Send
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
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 915 times.
Similar Threads
Compilation Error...
Which one of these two programs execute fast
constructor question
Compilation Error
importance of parameter
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:19:27.