answersLogoWhite

0

Static keyword in java

Updated: 8/11/2023
User Avatar

Wiki User

13y ago

Best Answer

A Static method in Java is one that belongs to a class rather than an object of a class. Normal methods of a class can be invoked only by using an object of the class but a Static method can be invoked Directly.

Example:

public class A {

.....

public static int getAge(){

....

}

}

public class B {

.....

int age = A.getAge();

}

In class B when we wanted the age value we directly called the method using the instance of the class instead of instantiating an object of the class.

Tip:

A static method can access only static variables. The reason is obvious. Something that is common to a class cannot refer to things that are specific to an object...

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
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

13y ago

The static keyword can be used with both methods and variables in java.

Static Methods

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();

Static Variables

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

13y ago

Static Inner Classes

Actually static inner classes aren't inner classes at all, by the standard definition of an inner class. While an inner class enjoys that special relationship with the outer class (or rather the instances of the two classes share a relationship), a static MyStaticExInnerClass class does not. It is simply a non-inner (also called "top-level") class scoped within another. So with static classes it's really more about name-space resolution than about an implicit relationship between the two classes.

A static MyStaticExInnerClass class is simply a class that's a static member of the enclosing class:

class MyExOuter {

static class StaticInner { }

}

The class itself isn't really "static"; there's no such thing as a static class. The static modifier in this case says that the MyStaticExInnerClass class is a static member of the outer class. That means it can be accessed, as with other static members, without having an instance of the outer class.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

static member means the method's behaviour does not depend on the state of an object.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: 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