aspose file tools*
The moose likes Beginning Java and the fly likes static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variables" Watch "static variables" New topic
Author

static variables

suganya gayathry
Greenhorn

Joined: Jun 16, 2007
Posts: 5
I want to know clear picture about static variables and static members so that i can not forget it again..
Akshayan Venkatesh
Greenhorn

Joined: Apr 13, 2006
Posts: 26
____________________________________________________________________
class StaticDemo
{
static String name = "static"; // static variable
static void show()
{
System.out.println("This is comes from static method");
}
public static void main(String []args)
{
System.out.println(name);
show(); // here we calling show method
}
}

in the above simple program we can access the variables and methods without Object.

If you declare member as static directly we can call the variables or methods if they are in the same class
____________________________________________________________________________
if you are using different classes we may use follows.

class Other
{
static void show()
{
System.out.println("other class");
}
}
class OtherDemo
{
public static void main()
{
show(); /// error
Other.show();
}
}


In this example we cannot call show() method. because it is in other class. so only you can access classname.membername(Other.show())

without creation of an Object we can access the members(variables & methods) of that class.

if any doubt/mistakes please mail to
ilanji83@gmail.com
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
For me, I have to "dumb it down" so my two brain cells don't argue over it.

if a member is declared static then only one copy of that member (variable or method) exists and it will be shared by any and all instantiations of that class. ALSO, a static can be referenced without there ever having been an instantiation of that class MADE.
example:

int i = ClassName.staticVariableName;

if a member is NOT declared static then each and every individual instantiation of that class will have it's own unique copy of that member (either variable or method) and any references to or operations performed on that member will be to that one instance ONLY. ALSO, to reference a non-static instance) member, the class MUST be instantiated and the reference made using that reference variable.
example:

ClassName myClass = new ClassName();
int i = myClass.nonStaticVariableName;

and, just to clarify, if you DO have multiple instantiations of a class with a static variable, and you modify that variable, then ALL instantiations will see the newly modified version.... since the variable is actually shared.

If my thinking is correct, methods are always shared between instantiations even if NOT statically declared. This can be done because they are purely re-entrant code so each thread through them is unique and private to that thread. Thus, making a method static only serves to make it accessible without instantiation. If a non-static method is declared then it must be called referencing a specific instantiation.

example:

ClassName myClass = new ClassName();
int i = myClass.nonStaticMethod1();

if a method is declared static then you may call it using the class name directly

example:

int i = ClassName.staticMethod2();
[ June 16, 2007: Message edited by: Bob Ruth ]

------------------------
Bob
SCJP - 86% - June 11, 2009
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
If I remember correctly: "static" members are called "static" because their memory location is static. A static member is loaded into memory when the class is loaded, and stays there. So it is only loaded once, with links from the Class object, so one can gain access by writing "MyClass.myStaticVariable" or "MyClass.myStaticMethod()".
You are correct that all instances of the class have access to that one static variable, that they share it, and a change is seen by every instance.
Instance variables have "dynamic" memory; they are loaded into memory when instantiated and can be unloaded when they are finished with. We don't usually say "dynamic" or "unloaded" however, we say "instance" and "garbage collected."
Because instance variables are found in different memory locations and loaded after all the static members, the static members do not have access to instance members. That is why you get the compiler errors if you try to gain access to an instance member (only it says "non-static") from a static method.
Yes, you are right that there is only one copy of each method; this is included with the Class object, and every time an instance method is called, a reference to the location in memory of that instance is passed too. More details about loading code into memory (for example) here in the Jakarta BCEL project manual. You probably want section 2.

. . . and welcome to the Ranch, suganya gayathry.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Campbell Ritchie:
If I remember correctly: "static" members are called "static" because their memory location is static.


Sorta kinda.

Some of the Java keywords are a bit strange, because the language designers chose to use the same keywords as C/C++ wherever possible, even if that meant "bending" their original meaning. I guess the idea was to make Java programs look familiar to C/C++ programmers, even if there were in fact subtle differences. The "static" and "volatile" keywords are examples of this.

Java says little or nothing about where objects are stored in memory, and I believe that objects are allowed to move around as the JVM implementation sees fit. Their object references have to stay the same, but an object reference is not a direct memory location (some JVMs may implement it as such, but that's just an implementation choice).

Personally, I think it's best to forget about the plain-English meanings of the keywords and just learn exactly what they mean in Java.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2718
    
    6

Hi,
All the above posts seem to say a lot about static members and static fields
so i think it sould be quite clear now.
I would only like to say that for me static means bound to the class itself and not to objects of the class.
They are generally used to describe properties that are specific to the class and are common to all objects.

As simple as that..

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2718
    
    6

Hi,
All the above posts seem to say a lot about static members and static fields
so i think it sould be quite clear now.
I would only like to say that for me static means bound to the class itself and not to objects of the class.
They are generally used to describe properties that are specific to the class and are common to all objects.

As simple as that..

Hope this helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variables