Java - static keyword in Java with example

Java static keyword is used to create a Class level variable in java. static variables and methods are part of the class, not the instances of the class.
The static keyword in Java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks, and nested classes.

static is a non-access modifier in Java that is applicable for the following:

  • blocks
  • variables
  • methods
  • nested classes
  • Interface static method(Java 8 onwards)


Java static variable

We can use static keyword with a class-level variable. A static variable is a class variable and doesn’t belong to the Object/instance of the class.
Since static variables are shared across all the instances of Object, they do not thread-safe.
Usually, static variables are used with the final keyword for common resources or constants that can be used by all the objects.

public class KnowledgeFactoryStaticDemo {

// static variable example
private static int count;
public static final String DB_USER_NAME = "knowledge@factory.com";
}



Java static method

Same as a static variable, the static method belongs to the class and not to class instances.
A static method can access only static variables of class and invoke only static methods of the class.

Usually, static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance. For example Collections class.

Java Wrapper classes and utility classes contain a lot of static methods. The main() method that is the entry point of a java program itself is a static method.

public class KnowledgeFactoryStaticDemo {
// static util method example
public static int addNumbers(int num1, int num2) {
return num1 + num2;
}
}




Java static block

If you need to do the computation in order to initialize your static variables, you can declare a static block that gets executed exactly once, when the class is first loaded.

public class KnowledgeFactoryStaticDemo {
// static block
static {
System.out.println("Static block initialized.");
}
}



Java static class

We can use static keyword with nested classes. the static keyword can’t be used with top-level classes.
A static nested class is the same as any other top-level class and is nested for only packaging convenience.

public class KnowledgeFactoryStaticDemo {
// static block
static {
System.out.println("Static block initialized.");
}

// static class example - used for packaging convenience only
public static class MyStaticClass {
public int count;
}
}

Comments

  1. Seo services the core of a service-based company; search engine optimization specialist knows how to search engine optimization assistance http://winthecustomer.com/successful-business-marketing-working-together-common-goal/. Seo organization has got the practical experience to assist you in objective your very own suitable buyer, find more targeted traffic and generate sales opportunities for that income crew.

    ReplyDelete
  2. They contributed many ideas and suggestions that made it a success, user interface design companies team consists of fantastic communicators, easy to figure with people, and resourceful project managers.

    ReplyDelete
  3. I wanna thanks to a great extent for providing such informative and qualitative material therefore often.
    find this

    ReplyDelete
  4. It’s really such nice information to get advantage from.
    UX design company

    ReplyDelete
  5. Their workflow is smooth and they're in constant contact with the client. The niche edit services team made it apparent they were working to achieve the client's goal.

    ReplyDelete
  6. The team’s ability to manage complex inputs with discipline and creativity allows them to meet high standards.
    brand building agency

    ReplyDelete

Post a Comment

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String

Top 5 Java ORM tools - 2024