Keywords in Programming Languages are the reserved words that you cannot use as a variable name or identifier because they have been already defined in the language either as a parameter or a statement.
In java, there are 57 keywords or reserved names and โStaticโ is one amongst them. Let us talk more about this static keyword with this table of content.
Let us understand the static keyword and its implementation starting with…What is static used for In Java?
Static keyword can be used within blocks of code, methods, variables, and classes by preceding the member name with โstaticโ.Now letโs move on to learn how to use this keyword with each member, starting with
Now that you have understood why we use static keywords, let us see its use cases, beginning with Static Block.
Static Block In Java
The static block is like a constructor, where you can define default values, except that values for all the objects will be the same. The static block gets executed only once, whereas the constructor will be executed as per
the number of objects. A static block can be accessed with an object.
Let me show you an example
// Java program to demonstrate static blocks
class Main
{
// static variables
static int a = 10;
static int b;
// static block
static {
System.out.println("changing the b value under the static block.");
b = a * 10;
System.out.println();
}
public static void main(String[] args)
{
System.out.println("under the main function:");
System.out.println("The value of a is "+a);
System.out.println("The value of b is "+b);
}
}
changing the b value under the static block.
under the main function:
The value of a is 10
The value of b is 100
// Java program to demonstrate static blocks
class StatTest {
static int a;
int b;
static {
a = 10;
System.out.println("static block executed ");
}
StatTest(){
System.out.println("Constructor executed");
}
}
class Main {
public static void main(String args[]) {
StatTest st1 = new StatTest();
StatTest st2 = new StatTest();
}
}
static block executedย
Constructor executed
Constructor executed
Static Variable In Java
Static variables can be used to define a common property for all the objects, like a common college name for the students, or the common company name of several employees.
Letโs understand the static variables in java with a token program example. The following program is supposed to give a new token without the use of static variables.
/*Java Program to demonstrate that instance variable get memory each time when we create an object of class */
class Token {
int count = 0; //gets memory every time, instance is created
Token() {
count++; // incrementing TOken value
System.out.println(count);
}
public static void main(String args[]) {
// Creating Token objects
Token c1 = new Token();
Token c2 = new Token();
Token c3 = new Token();
Token c4 = new Token();
}
}
1
1
1
1
/*Java Program to demonstrate that static variable */
class Token {
static int count = 0; //gets memory once,and retains the value
Token() {
count++; // incrementing TOken value
System.out.println(count);
}
public static void main(String args[]) {
// Creating Token objects
Token c1 = new Token();
Token c2 = new Token();
Token c3 = new Token();
Token c4 = new Token();
}
}
1
2
3
4
Hope you understood static variables in java with the above program, so next up we have static methods in java.
Check out this Complete Java Online Training by FITA. FITA provides a complete Java course including core java and advanced java J2EE, and SOA training, where you will be building real time applications using Servlets, Hibernate Framework, and Spring with Aspect-Oriented Programming (AOP) architecture, Struts through JDBC bundled with, placement support, and certification at an affordable price with an active placement cell, to make you an industry required certified java developer.
Static methods in java
Declaring methods static, would not require you to create an object and then call the method, and instead, you can refer to them using class methods because static methods are of class rather than the objects of the class.
Here is an example to demonstrate the static method in java.
class Company {
int salary;
String name;
String emp_type;
static String company_name = "FITA";
// static method for changing the value of a static variable
static void companyname(String c) {
company_name = c;
}
// constructor to initialize the variable
Company(int r, String n, String t) {
salary = r;
name = n;
emp_type = t;
}
// method to display values
void display() {
System.out.println(name + ":" +salary + ":" + company_name);
}
public static void main(String args[]) {
Company.companyname("FITA Academy");// calling change method to change company name
// creating instances of class Company
Company e1 = new Company(50000, "Shaneela","developer");
Company e2 = new Company(20000, "Kartick","finance controller");
Company e3 = new Company(30000, "Shareef","digital marketer");
// calling display method
e1.display();
e2.display();
e3.display();
}
}
Shaneela:50000: FITA Academy
Kartick:20000: FITA Academy
Shareef:30000: FITA Academy
You might have observed that the main method is declared static so thatย JVM does not require to create an object to invoke the main method, which also makes it memory efficient.
There is a restriction that the static methods cannot use or invoke non-static data members or methods directly nor can you use the this and super keyword in a static context. Let me show you an example of this.
class TestStat {
int a = 20;// non static variable
public static void main(String args[]) {
System.out.println(a);
}
}
// Output for the above program
/*Main.java:5: error: non-static variable a cannot be referenced from a static context
System.out.println(a);*/
Static Class In Java
Only a nested class can be made static so that nested classes do not need a reference to an outer class. Although inner classes can access all the static and non-static members, the static class can only access static members of the class. Let me show you an example.
public class Main {
private static String name = "FITA";
private String str= "Academy";
// Static class
static class Employee {
String com_name;
Employee(String t) {
com_name = t;
}
// non-static method
public void display() {
// System.out.println(str);
System.out.println(name);
System.out.println(com_name);
}
}
public static void main(String args[]) {
Main.Employee e1 = new Main.Employee("Academy");
e1.display();
}
}
FITA
Academy
The commented line to print str would cause an error saying, โnon-static variable name cannot be referenced from a static contextโ if executed.
This was all about a static block, static method, static variables, and static classes in java, their implementations and use cases along with example programs. To get in-depth knowledge of core Java and advanced java, J2EEย SOA training along with its various applications and real-time projects using Servlets, Spring with Aspect-Oriented Programming (AOP) architecture, Hibernate Framework, and Struts through JDBC you can enroll in Certified Java Training in Chennai or Certified Java Training in Bangalore by FITA or a virtual class for this course, at an affordable price, bundled with real-time projects, certification, support, and career guidance assistance and an active placement cell, to make you an industry required certified java developer.
FITAโs courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.