Static means you can access the variable or method without creating an instance.It is connected with the class and not with any particular object.It can be accessed by all the objects of the class. [This message has been edited by SenthilNathan CAlagan (edited June 25, 2001).]
static modifiers specifies that the variable is a class level variable. Every instance of the class (i.e object) will access a single copy of static variable. It can be accessed using classname.variablename or object.variablename. Hope this Helps. Gautam. ------------------
Angela Jessi
Ranch Hand
Joined: Nov 27, 2000
Posts: 428
posted
0
So for example: If I have two classes A and B If I declare variable and method as static in class A: static int i = 0; (VARIABLE) static getMsg(); (METHOD) Now If I want to access this method static getMsg() or static int i in class B can I access??? Thanks Angela
In B you can say int newVar = A.i; the method that you declared in A needs a return type but static void getMsg(); (METHOD) can be accessed using A.getMsg();
"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Oh yeah, you COULD created an instance of class A and get at the static stuff through that - but try to avoid that - it gets way to confusing to the rest of us. We then tend to forget that fact that these things are static and abuse them. There is never any reason that you HAVE to use the following syntax, but you could. A myInstance = new A(); myInstance.i; myInstance.getMsg();
Angela Jessi
Ranch Hand
Joined: Nov 27, 2000
Posts: 428
posted
0
If it not static method, we access the sameway for non-static method. Then what is difference between static and non-static methods? Thanks Angela
Originally posted by Cindy Glass: Oh yeah, you COULD created an instance of class A and get at the static stuff through that - but try to avoid that - it gets way to confusing to the rest of us. We then tend to forget that fact that these things are static and abuse them. There is never any reason that you HAVE to use the following syntax, but you could. A myInstance = new A(); myInstance.i; myInstance.getMsg();
Non-static and static methods are referred to in the same way from an *instance* of the class.
The advantage of static methods is that you do not *need* an instance of the class.
So if you are referring to a static method, use the Class name... Like This could have been written like this:But then, how can a human reader, by looking at the code, determine that parseInt is static? We needlessly create an Integer object, and we have hidden the static nature of the method by using it through an instance.
another thing which i like to tell that when no any object then u can call the all static variables and methods by class name ie. classname.staicvar ; classname.staicmethod ;
but for accessing the instance members u must need to create object . The example of static is like that some thing are still in all the human which must denote this is the property fo a human like hand,leg,and eat and talk etc i think all these are static properties. when u are talking about the human 1- u say human has two hands not that a particular person only has hands(means denote by the class name ) 2- after the creation of the human u can also say that he has two hands means he is related to a family whose all object must has that same state and behaviour. i think it will clear u more difference between the static and intance memebers and why we call static ,by class name and instance object refernce. junaid
junaid rehman
Greenhorn
Joined: Jun 17, 2001
Posts: 16
posted
0
another thing which i like to tell that when no any object then u can call the all static variables and methods by class name ie. classname.staicvar ; classname.staicmethod ;
but for accessing the instance members u must need to create object . The example of static is like that some thing are still in all the human which must denote this is the property fo a human like hand,leg,and eat and talk etc i think all these are static properties. when u are talking about the human 1- u say human has two hands not that a particular person only has hands(means denote by the class name ) 2- after the creation of the human u can also say that he has two hands means he is related to a family whose all object must has that same state and behaviour. i think it will clear u more difference between the static and intance memebers and why we call static ,by class name and instance object refernce. junaid
Angela Jessi
Ranch Hand
Joined: Nov 27, 2000
Posts: 428
posted
0
Thanks to all, If member or function of the class is static, should I have to declare class as an static also?
Thanks angela
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
There is no such thing as a static class. That would be like saying "This class is a class level class". Static means for a variable - there is only one per class not one per instance for a method - that the method can be executed without the use of an instance of the class.
Then why we use static in public void static main() method for java application
Originally posted by Cindy Glass: There is no such thing as a static class. That would be like saying "This class is a class level class". Static means for a variable - there is only one per class not one per instance for a method - that the method can be executed without the use of an instance of the class.
static makes data, methods, blocks and classes exist per class; not per instance. main() is a static method because that is where application execution begins and therefore it has to be class wide (per class). static classes are nested top-level classes and must be within another class. The entire static class (constructors, methods, and fields) is a member of the inclosing class.
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Nancy, main is a method, not a class. Also, this in not Meaningless Drivel. Please change your name to be compliant with JavaRanch's naming policy. Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name. Thanks, Cindy
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.