answersLogoWhite

0


Best Answer

When we want that a class member should be used independently of any object of that class, we have to precede that member's declaration with the keyword static .
When a member is declared as static, it can be acccessed without creationg an object of the class and without referring to the object.That static is , a static member belons to a class as a whole and not to any one instance/object of that class.
The static member is shared by all objects of thatclass.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

The same keyword "static" is used a number of different ways in Java.

1. For defining class constants:

public class A {

private static final MINIMUM int = 1000;

}

The above line of code defines a constant. The "static" part means that there is only one for the class. This is desirable because you could have a million objects but the constant is always the same so it is declared "static" because only one is needed.

2. Methods can be declared "static" which means that an object is not required to execute the method. For example, if you want to take the absolute value of a primitive, you wouldn't want to create an object unnecessarily.

int i = Math.abs(-1);

Here, if you look at the definition of the Math.abs function, it is static. Notice that because there is no object, you can call a static method using the name of the class, "Math", instead of using the name of an object.

3. Sometimes, you have a class variable and no matter how many objects of this class that you have, you only want one variable for the entire class. For example, if you are developing a desktop application with Java and you want to have an object of type Application, the software should only have one Application object. This is a design pattern called a 'Singleton' which should be rare because usually it's best to do object-oriented programming.

"static" usually means you're doing things the "old fashioned" way before programming languages were object oriented. The rule nowadays is to use object-oriented programming, but there are times to use the static keyword and do things without so many objects.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

The word static is usually used to specify the properties of a class that are common to a class and not its objects. The properties may include variables and/or methods.

If you have a static method in a class then you can directly access the method using the class name. You need not instantiate an object of the class to access the method. This is how we do it for normal methods but for Static methods this is not required.

Example:

public class A {

....

public static String getDate(){

....

}

.......

}

public class B {

...

String date = A.getDate();

....

}

In class B we have called the getDate() method in class A without instantiating an object of class A.

Tip:

Static methods can only access static variables. The reason is simple. If a variable is not static, then it would not be initialized until the class is instantiated. Hence the method cannot use it.

Note:

Using static variables & methods should be avoided as much as possible because since these properties of the class are not tagged to any instance of the class, these properties would be available in the memory until the class instance is destroyed. This is not the optimal way to use memory. Hence static variables and methods should be used only when highly required.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The Static keyword signifies the fact that, the variable or method under consideration belongs to the class and not any of its instances. So you can access them without any object reference of the class.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

"static" has slightly different meanings, depending on where it is used.

A static field, in a class, is a variable that exists only once for the entire class, no matter how many objects are created, based on that class. For example, the built-in Math class has the important numbers, E and PI, defined as static fields. (They are also defined as final, so they can't be changed later on.)

A static method is a method that doesn't access non-static (i.e. instance) member data. For example, all the methods in the Math class are defined as static. This means they can be invoked directly with the class name, you don't need to create an instance (object) of the class. The following example shows the square root of two:

System.out.println(Math.sqrt(2.0));

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The keyword static means that a field is stored only once for a class, no matter how many - or how few - objects are created on the basis of the class.

Example 1: The Math class has static fields that store the numbers e, and pi.

Example 2: You might create a static variable to keep track of how many objects you have created, based on a class. A single copy of this variable (or field) is appropriate in this case.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Using on static keyword, we can write static methods and variables. In java static variables accessing class level, and coming to the static methods without creating the object we can access methods.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Static is a java keyword that signifies the fact that the variable or method that is declared static belongs to the class and not any instance of the class (Object)

Static keyword when used with a method, specifies that this method belongs to the class and not a particular instance of the class (a.k.a object of the class)

Ex:

public class StaticTest {

public static String getAuthorName() {

return "Anand";

}

}

Here getAuthorName is the static method and it can be accessed without instantiating an object of the class StaticTest. You can access this method as:

String authorName = StaticTest.getAuthorName();

The static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A "static" field is shared among all objects in a class, as opposed to every object having its own copy of the field. Also, a static field requires no object - it can be accessed directly with the class name. This can be used, among other things, when you need constants that are required only once, no matter how few or how many objects you create. As an example, the mathematical constants "e" and "pi" are defined as static in the "Math" class. Also, this can be used whenever you need to share some variable amongst several objects. For example, you might want to keep track of how many objects you created, based on a specific class. To do this, in the class constructor you increment a static variable.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Advantages of static keyword in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

Are static variable created by giving keyword static in java?

yes bcoz static variables


Can you declears static in a local variable in a method in java?

we cannot use the staic keyword inside the method... But we can use the final keyword inside the method....


Why do you use static key word in Java programming?

Static is a keyword,when u execute a method in class file loaded, execute the first this method..


Is check a keyword in java?

No, 'check' is not a keyword in java language.


Is float is a keyword in java?

yes, float is keyword and data type in java


What are static objects in java?

There is no separate entity as a static object in java. The static keyword in java is used to signify that the member (either a variable or a method) is not associated to an object instance of the class. It signifies the fact that the member belongs to the class as a whole. The words static and objects are opposites of one another so you cannot have a static object. However, you can declare an object as a class level variable which could be referred to as a static object but it will be referred to as a static or class variable and not a static object.


Why a static member method can access only static members in java?

Because, the keyword static signifies the fact that the method or variable that is qualified using the static keyword is not attached to any object of the class. Therefore we cannot instantiate the class and use the object to reference to access it. The only option we have is to use the class name to directly access them


What does the verify keyword do in Java?

"verify" is not a Java keyword. I believe the link, in related links, has the complete list of Java keywords.


What is foreign keyword in java?

There is no "foreign" keyword in Java, however, there is a native keyword that declares native methods in a native language, such as C or C++.For full list of keywords in Java see related question.


Tell about static method?

in java a method is said to be static if 'static 'keyword is used before the method name . foe ex.- static void show(){ ........ } this method has the following property-- 1. it can invoke only a static method. 2. it can't be reffered using keyword 'this','super'. 3.static method can access only a STATIC MEMBER VARIABLE or STATIC CLASS VARIABLE . 4. there should not be static & non static version of a nethod in a class . 5.static method can be used before the creation of d object of dt class.


What is literal in java programming?

Literal in java are L, F, null, true, false These act as keyword(have special meaning in java) but these does'nt comes under the category of Java Keyword.


What is java's keyword for the integer data type?

"int" is the keyword for integer