• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Method

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is Static modifier indicates? What is use of it?
Thanks,
Angela
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();


 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is why Cindy said it's confusing.

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.
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all
Angela
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic