English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2006-07-04 18:26:36 · 7 answers · asked by kiran k 1 in Computers & Internet Programming & Design

7 answers

Static methods use no instance variables of any object of the class they are defined in. When we use static we don't need to create any instance of that class. Whenever we import that static class in our java file, we only need to access it by name not by instance creation.

Treat static as a Global function. You define somthing static when you don't need to create an instance of that object. If you don't need anything to be saved as an object, creating something not static defeats the purpose of object oriented design.

So if you create an instance of the class and a method within that class is static an error message by the compiler would exist since that defeats the purpose.

You can access static variables, but except for constants, this is unusual. Static methods typically take all they data from parameters and compute something from those parameters, with no reference to variables.

A typical EXAMPLE of static is the Math Functions. The math functions are all static methods. You do not have to do Math myfunc = new Math() // That is creating an instance.
You just access the methods directly. See why we use static? IF we don't need to preserve the values or instance no need to create an instance we just declare it as static!

Good Luck!

2006-07-04 18:31:20 · answer #1 · answered by ? 6 · 1 0

static
static variables and methods might better have been called perClass variables and methods. They inherited this misleading terminology from C++. They are the opposite of instance variables and methods that work on a particular object.
There is nothing static (unchanging) about them. They don't cling. They are perfectly clear, unlike radio signals garbled by static.

They are allocated when the class is loaded. static refers to a method or variable that is not attached to a particular object, but rather to the class as a whole.

static final when applied to a variable is Javanese for "constant". All static methods are automatically final. It is not strictly speakinvg an error to mark them final, but it is redundant and considered bad form.

static methods work without any this object. static methods are limited to calling other static methods in the class and to using only static variables. They can call instance methods only if they use their own object references -- not rely on this .

static methods and variable are in a sense inherited, but not in the same strong sense that instance variables and methods are. You can refer to Dog.bark() as Dalmatian.bark() if no one has written a Dalmatian.bark(). However, if you use Dog.bark() you always get the Dog version and if you say Dalmatian.bark() you always get the Dalmatian version.

Newbies tend to overuse static variables. Consider what would happen if your code were used by several threads simulaneously. With shared static variable they would trip over each other. With local and instance variables they often would not, even without any special sychronisation. Sometimes, of course, you do need the globalness of static variables, but don't use it where it would make more sense to create a object to track each chain of calculation.

Static typing
The word static is used in a second context, the opposite of dynamic or runtime type. This refers to the compile-time declared type of a variable, compared with the run time actual type it points to. e.g. a Dog variable may point to a Dalmatian object, but not vice versa. The static type (the type of the reference) is Dog and the dynamic type (the type of the object) is Dalmatian. You will often hear Java referred to a language with static type checking. The types of all references are checked for consistency at compile time.
Static Loading
The word static is used in a third context. It refers to ordinary classes explicitly mentioned in the code. Dynamic classes are loaded on-the-fly using Class.forName where the class name is constructed as a String.
Static Web Pages
The word static is used in a fourth context. It refers to web pages the server sends out unchanged. Dynamic pages are modified or even composed from scratch before sending.
Static Nested Classes
Finally, Java has an obscure feature where you can also declare nested classes static. This does not mean all their methods are static, or that they cannot be instantiated, rather it means you instantiate the inner classes independently of the main enclosing class. There is no associated outer class object. Such classes are often called nested static classes. Non-static inner class objects always have an associated outer class object.

2006-07-05 06:52:55 · answer #2 · answered by kunal_oncyber 2 · 0 0

The word static is used in a second context, the opposite of dynamic or runtime type. This refers to the compile-time declared type of a variable, compared with the run time actual type it points to. e.g. a Dog variable may point to a Dalmatian object, but not vice versa.

2006-07-05 01:43:08 · answer #3 · answered by Adeel I 3 · 0 0

HI
JVM allocates memory to static variables at the start of the execution only,
so they are globalized.
so we can call that variables or methods without using any object.
and they will e accessed at any where of the program.

2006-07-05 01:37:21 · answer #4 · answered by praveen s 1 · 0 0

static keyword refers to the memory allocation in java.
means if a variable declared as static it only have one memory
space to store data.

2006-07-05 01:32:15 · answer #5 · answered by jacky 2 · 0 0

static means that this member of class can be accessed from outside the class without making object of that class
like we have a static member ACB in class XYZ
we can access it as XYZ.ABC

2006-07-05 01:40:21 · answer #6 · answered by Tasawer Khan 2 · 0 0

Everything about static

java-latte.blogspot.in/2014/01/what-is-static-keyword-in-java.html

2014-03-11 05:43:26 · answer #7 · answered by pardeep 1 · 0 0

fedest.com, questions and answers