Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

static

  Asked By: Fern    Date: Nov 21    Category: Java    Views: 676
  

Do you know how and when to use a "static variable" and "static method"? Any
examples?

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Sherrie Thomas     Answered On: Nov 21

static  variable or method  is one
that exists without needing to
create an instance of the class.
There are only a few cases where
it is justified to use them;
overusing them destroys the meaning
of objects. Static variables are
used when there is a single instance
of the variable  to be used for all
instances of the class. Static
methods are used when there is
some action to perform that relates
to the class but does not act upon
an instance of it. In fact, static
methods cannot access an instance
of the class and can only make
use of static variables or other
static methods. That's a pretty
brief summary. Does it help?

 
Answer #2    Answered By: Anselma Schmidt     Answered On: Nov 21

Static variables or methods are used to deal with the calss variables
and not with instance variables.
To be more clear the main method  in Java is itself a static  method
in a class and the main method is declared as static because it has
to be called without creatinhg an instance of the class.
Similarly if you want any variable  or method belonging to that class to be
called
by the main method or any static method for that matter then you will have to
declare them as static.
At the same time a method declared as static can create instances of the class
and use them.

 
Answer #3    Answered By: Dang Tran     Answered On: Nov 21

Static variables or methods are used to deal with the calss variables
and not with instance variables.
To be more clear the main method  in Java is itself a static  method
in a class and the main method is declared as static because it has
to be called without creatinhg an instance of the class.
Similarly if you want any variable  or method belonging to that class to be
called
by the main method or any static method for that matter then you will have to
declare them as static.
At the same time a method declared as static can create instances of the class
and use them.

 
Answer #4    Answered By: Jamie Roberts     Answered On: Nov 21

Basicly anything the Object always has to know and can never change (no
matter how many of those objects you call) can be static.

A good example of a static  method would be something like the command
list for a FTP server session. No matter how many sessions with clients
that server has, every single command that it can accept is exactly the
same and can never change.

The main method  is the most common example of a static method, basicly
it can be run before the rest of the Object is constructed (they normaly
construct the constructor for a runnable class).

You can have plenty of static methods, for example the Maths class makes
good use of many static methods, such as Maths.random(). No new input or
variables can be inserted into Maths.random() and it doesn't need you to
construct a maths object (ie you don't need Maths maths = new Maths();
maths.random();)

 
Didn't find what you were looking for? Find more on static Or get search suggestion and latest updates.




Tagged: