static keyword in java

Easy USe of Static keyword in java

Posted on

Static keyword in java

The Static keyword in java in This is used for memory management mainly. we will apply Static keyword in java with variables, methods, blocks, and nested classes. The Static keyword in java belongs to the category than an instance of the category.

image host

The static can be:

  • Variable (also referred to as a category variable)
  • Method (also referred to as a category method)
  • Block
  • Nested class

For example, within the This program, we are accessing static method m1() without creating any object of sophistication.

// This program to demonstrate that a static member
// are often accessed before instantiating a category
class Test
{
// static method
static void m1()
{
System.out.println(“from m1”);
}

public static void main(String[] args)
{
// calling m1 without creating
// any object of sophistication Test
m1();
}
}

Output:

from m1

1) This static variable

If you declare any variable as static, it’s referred to as a static variable.

The static variable are often wont to ask the common property of all objects (which isn’t unique for every object), for instance, the corporate name of employees, the school name of scholars, etc.

The static variable gets memory just one occasion within the class area at the time of sophistication loading.

Advantages of static variable

  • This improves the memory of your program.
  • Understanding the matter without static variable

class Student{  

     int rollno;  

     String name;  

     String college=”ITS”;  

}  

Suppose I even have 500 students in college, now that everything is going to be remembered by data members whenever a unit is made.

All students have their own unique number and name on the list, so it’s best to use a knowledge item during this situation. Here “college” is the common property of all objects. If we stabilize it, this area will only receive memory once.

Below the This program shows that static blocks and configurations are performed the way they display within the program.

// This program to demonstrate execution
// of static blocks and variables
class Test
{
// static variable
static int a = m1();

// static block
static {
System.out.println(“Inside static block”);
}

// static method
static int m1() {
System.out.println(“from m1”);
return 20;
}

// static method(main !!)
public static void main(String[] args)
{
System.out.println(“Value of a : “+a);
System.out.println(“from main”);
}

}

Output:

from m1
Inside static block
Value of a : 20
from main

Static blocks ::

If you would like to try to to the computation so as to initialize your static variables, you’ll declare a static block that gets executed exactly once, when the category is first loaded.

Consider the subsequent This program demonstrates the use of static blocks.

//  This program to demonstrate the utilization of static blocks
class Test
{
// static variable
static int a = 10;
static int b;

// static block
static {
System.out.println(“Static block initialized.”);
b = a * 4;
}

public static void main(String[] args)
{
System.out.println(“from main”);
System.out.println(“Value of a : “+a);
System.out.println(“Value of b : “+b);
}
}

Output:

Static block initialized.
from main
Value of a : 10
Value of b : 40

 

Static methods OF Static keyword in java::

When a path is said by a Static keyword in java, it’s called a vertical path. the foremost common example of a static method is that the main method () as mentioned above, any static member is often reached before anything is made within the class and maybe mentioned any object. Methods declared as static have several limitations:

  • They can directly call the alternatives.
  • They can directly access static data.
  • They can’t ask this or super in any way.

Below this program is to point out the bounds on static methods.

// This program to demonstrate restriction on static methods
class Test
{
// static variable
static int a = 10;

// instance variable
int b = 20;

// static method
static void m1()
{
a = 20;
System.out.println(“from m1”);

// Cannot make a static regard to the non-static field b
b = 10; // compilation error

// Cannot make a static regard to the
// non-static method m2() from the sort Test
m2(); // compilation error

// Cannot use super during a static context
System.out.println(super.a); // compiler error
}

// instance method
void m2()
{
System.out.println(“from m2”);
}

 

public static void main(String[] args)
{
// main method
}
}

When to use static variables and methods?

Use static variables for properties that are common to all or any objects. for instance, during a class, all students have an equivalent name. Use a uniform approach to modifying static changes.

Consider the subsequent program, which demonstrates the utilization of static keys with variables and methods.

// A This program to demonstrate use of
// Static keyword in java with methods and variables

// Student class
class Student
{
String name;
int rollNo;

// static variable
static String cllgName;

// static counter to line unique roll no
static int counter = 0;

public Student(String name)
{
this.name = name;

this.rollNo = setRollNo();
}

// getting unique rollNo
// through static variable(counter)
static int setRollNo()
{
counter++;
return counter;
}

// static method
static void setCllg(String name){
cllgName = name ;
}

// instance method
void getStudentInfo(){
System.out.println(“name : ” + this.name);
System.out.println(“rollNo : ” + this.rollNo);

// accessing static variable
System.out.println(“cllgName : ” + cllgName);
}
}

//Driver class
public class StaticDemo
{
public static void main(String[] args)
{
// calling static method
// without instantiating Student class
Student.setCllg(“XYZ”);

Student s1 = new Student(“Alice”);
Student s2 = new Student(“Bob”);

s1.getStudentInfo();
s2.getStudentInfo();

}
}

Output:

name : Alice
rollNo : 1
cllgName : XYZ
name : Bob
rollNo : 2
cllgName : XYZ

Static classes entered OF Static keyword in java:

We cannot declare a premium with a static converter, but nested classes are static. These sorts of classes are called nested static classes. For a static nested class, Show Java Static Nested Class.

SUPER KEYWORD IN JAVA

THANK YOU

Write comments if you discover something wrong or want to share more information on the subject above.

One thought on “Easy USe of Static keyword in java

Leave a Reply

Your email address will not be published. Required fields are marked *