• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

static string

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables declared inside of a method can not be static.
------------------
Co-Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Can't can I declare static veriables in Static method as well??
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic