Static keyword in java

Static keyword in java

The static keyword in java is used for memory management in JAVA. Static keywords belong to the class rather than the instance/object of a class.
We can use static keywords with:
  1. Variable
  2. Method
  3. Block
  4. Class

1. Static variable in Java

You can declare a static variable by simply use of static keyword with a variable. 
  • Static variables are also known as a class variable because they are associated with the class and common for all the objects of a class.
  • Static variables are declared using the static keyword within a class outside any method/constructor/block.
  • When a class created then one copy of a static variable created in memory. Static variables would be common for all objects of class because a static variable is associated with a class. 
  • Static variables are created at the start of program execution and destroyed automatically when execution ends.
  • Initialization of Static Variable is not Mandatory. Its default value is depending on the data type of variable.
  • If we access the static variable through an object the compiler will show the warning message. The compiler will replace the object name to the class name automatically.
  • We can create static variables at class-level only.

2. Static method in Java

You can declare a static method by using of static keyword.
1. Static methods are belonging to a class.
2. You can access a static method by using the class name. You do not have the need to create an object.
3. A static method can access only static variables. If you write any non-static variable in a static method, then it will throw an exception at compilation time.
Why a static method is important
A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.
Ex: To know KM to Miles someone doesnt to build a car object. Car.convertKMtoMiles(25).
Static method can access without any object. They can access directly by class name. The most common example of a static method is main( ) method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.

3. Static block in Java

If you want to do some calculation and initialize the static variables you can use static block. The static block is get executed once when the class is first loaded.

4. Static class in Java

You can read the static class from my previous post
Krishna babu Ghanta
Mobile : 303-886-1490 
Time zone : CST

0 comments to "Static keyword in java"

Post a Comment

Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...


Powered by WidgetsForFree