123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

What Is The Static Keyword In Java

Profile Picture
By Author: KT brain
Total Articles: 1
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

The static can be:

1.variable (also known as class variable)
2.method (also known as class method)
3.block

4.static variable

If we declare any variable as static, it is known static variable.

.The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc. means a single copy to be shared by all instances of the class.

.The static variable gets memory only once in class area at the time of class loading.

.It is a variable which belongs to the class and not to object(instance)

.Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables

.A static variable can be accessed directly by the class name and doesn’t need any object

Advantage ...
... of static variable

It makes our program memory efficient (i.e it saves memory).

Ex:static variable

1.//Program of static variable

2.

3.class Employee11{

4.int empId;

5.String name;

6.static String company=“VBBS”;

7.

8.Employee11(int r,String n){

9.empId = r;

10.name = n;

11.}

12.void display (){System.out.println(empId+” “+name+” “+company);}

13.

14.public static void main(String args[]){

15.Employee11 e1 = new Employee11 (111,“Kiran”);

16.Employee11 e2 = new Employee11 (222,“Aryan”);

17.

18.e1.display();

19.e2.display();

20. }

21.}

static method

static keyword with any method, it is known as static method.

.A static method belongs to the class rather than object of a class.
.A static method can be invoked without the need for creating an instance of a class.
.static method can access static data member and can change the value of it.

Example : public static void main itself is a static method

class MyStatic{
static int i;
static String s;
public static void main(String args[]) //Its a Static Method
{

MyStatic obj=new MyStatic();
//Non Static variables accessed using object obj
System.out.println("i:"+obj.i);
System.out.println("s:"+obj.s);

}
}

Output:

i:0
s:null

Static method display()
class MyStatic1{
static int i;
static String s;
//Static method
static void display()
{
//Its a Static method
MyStatic1 obj1=new Mystatic1();
System.out.println("i:"+obj1.i);
System.out.println("i:"+obj1.i);
}

void funcn()
{
//Static method called in non-static method
display();
}
public static void main(String args[]) //Its a Static Method
{
//Static method called in another static method
display();
}
}

Output:

i:0
i:0

Restrictions for static method

There are two main restrictions for the static method. They are:

1.The static method can not use non static data member or call non-
static method directly.
2.this and super cannot be used in static context.

1.class A{
2.int a=40;//non static
3.
4.public static void main(String args[]){
5.System.out.println(a);
6. }
7.}

Static Block

Static block is mostly used for changing the default values of static variables.This block gets executed when the class is loaded in the memory.

A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.

.Is used to initialize the static data member.
.It is executed before main method at the time of class loading.

Example: static block

1.class A2{
2.static{System.out.println(“static block is invoked”);}
3.public static void main(String args[]){
4.System.out.println(“Hello world”);
5. }
6.}

Output:static block is invoked

Hello world

Total Views: 689Word Count: 646See All articles From Author

Add Comment

Computer Programming Articles

1. Your Complete Bugzilla Tutorial For Managing Software Bugs Efficiently
Author: Tech Point

2. From Beginner To Expert: Ultimate Jira Tutorial For Effective Team Collaboration
Author: Tech Point

3. Top Web Development Institutes In Bhopal: Where Creativity Meets Technology
Author: Kabir Patel

4. The Ultimate Framework Showdown: Which One Will Reign Supreme
Author: Andy

5. Why Your Competitors Are Investing In Custom Software (and You Should Too)
Author: Aimbeat Insights

6. The Hidden Security Risk Of Ssh Keys: Why Manual Linux Access Management Is A Ticking Time Bomb
Author: Tushar Pansare

7. Beyond Ticketing: Using Laravel And N8n To Automate Customer Onboarding Workflows
Author: Andy

8. Top Web Development Institutes In Bhopal: Turning Ideas Into Code
Author: Kabir Patel

9. Software Testing Tutorial: Learn Manual And Automation Testing With Easy Examples
Author: Tech Point

10. Ultimate Yii Framework Tutorial For Building Powerful Php Websites
Author: Tech Point

11. Java Job Support: Real-time Assistance For Developers To Succeed
Author: RKIT Labs Team

12. Unlocking Business Growth With Predictive Analysis
Author: Sakhi Kaya

13. What Is A Distributed Environment In Software Development?
Author: Aimbeat Insights

14. A Multi-tenant Admin Dashboard With Laravel And Next.js
Author: Andy

15. How To Choose The Data Science Training In Bhopal For Your Career Growth
Author: Kabir Patel

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: