Creating static class in java

Java – Static Class, Block, Methods and Variables

Static keyword can be used with class, variable, method and block. Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without object. Let’s take an example to understand this:

Here we have a static method myMethod() , we can call this method without any object because when we make a member static it becomes class level. If we remove the static keyword and make it non-static then we must need to create an object of the class in order to call it.

Static members are common for all the instances(objects) of the class but non-static members are separate for each instance of class.

class SimpleStaticExample < // This is a static method static void myMethod() < System.out.println("myMethod"); >public static void main(String[] args) < /* You can see that we are calling this * method without creating any object. */ myMethod(); >>

Static Block

Static block is used for initializing the 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.

Читайте также:  Цвета

Example 1: Single static block

As you can see that both the static variables were intialized before we accessed them in the main method.

class JavaExample < static int num; static String mystr; static< num = 97; mystr = "Static keyword in Java"; >public static void main(String args[]) < System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); >>
Value of num: 97 Value of mystr: Static keyword in Java

Example 2: Multiple Static blocks

Lets see how multiple static blocks work in Java. They execute in the given order which means the first static block executes before second static block. That’s the reason, values initialized by first block are overwritten by second block.

class JavaExample2 < static int num; static String mystr; //First Static block static< System.out.println("Static Block 1"); num = 68; mystr = "Block1"; >//Second static block static < System.out.println("Static Block 2"); num = 98; mystr = "Block2"; >public static void main(String args[]) < System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); >>
Static Block 1 Static Block 2 Value of num: 98 Value of mystr: Block2

Java Static Variables

A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.
Few Important Points:

  • Static variables are also known as Class Variables.
  • Unlike non-static variables, such variables can be accessed directly in static and non-static methods.
Читайте также:  Transition effect in css

Example 1: Static variables can be accessed directly in Static method

Here we have a static method disp() and two static variables var1 and var2 . Both the variables are accessed directly in the static method.

class JavaExample3 < static int var1; static String var2; //This is a Static Method static void disp()< System.out.println("Var1 is: "+var1); System.out.println("Var2 is: "+var2); >public static void main(String args[]) < disp(); >>

Example 2: Static variables are shared among all the instances of class

In this example, String variable is non-static and integer variable is Static. As you can see in the output that the non-static variable is different for both the objects but the static variable is shared among them, thats the reason the changes made to the static variable by object ob2 reflects in both the objects.

ob1 integer:99 ob1 String:I'm Object1 ob2 integer:99 ob2 STring:I'm Object2

For more details on refer: Java – static variable

Java Static Methods

Static Methods can access class variables(static variables) without using object(instance) of the class, however non-static methods and non-static variables can only be accessed using objects.
Static methods can be accessed directly in static and non-static methods.
Syntax:
Static keyword followed by return type, followed by method name.

static return_type method_name();

Example 1: static method main is accessing static variables without object

Example 2: Static method accessed directly in static and non-static method

class JavaExample < static int i = 100; static String s = "Beginnersbook"; //Static method static void display() < System.out.println("i:"+i); System.out.println("i:"+s); >//non-static method void funcn() < //Static method called in non-static method display(); >//static method public static void main(String args[]) < JavaExample obj = new JavaExample(); //You need to have object to call this non-static method obj.funcn(); //Static method called in another static method display(); >>
i:100 i:Beginnersbook i:100 i:Beginnersbook

Static Class

A class can be made static only if it is a nested class.

  1. Nested static class doesn’t need reference of Outer class
  2. A static class cannot access non-static members of the Outer class

We will see these two points with the help of an example:

Static class Example

class JavaExample < private static String str = "BeginnersBook"; //Static class static class MyNestedClass< //non-static method public void disp() < /* If you make the str variable of outer class * non-static then you will get compilation error * because: a nested static class cannot access non- * static members of the outer class. */ System.out.println(str); >> public static void main(String args[]) < /* To create instance of nested class we didn't need the outer * class instance but for a regular nested class you would need * to create an instance of outer class first */ JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass(); obj.disp(); >>

Источник

Static Class in Java

Java Course - Mastering the Fundamentals

In Java, static is one of the most popular keywords associated with functions, variables, classes, and blocks. When any of these members are made static, we can access it without creating an instance of the class in which they are defined.

Static class in Java is a nested class and it doesn’t need the reference of the outer class. Static class can access only the static members of its outer class.

static-class-accessing-static-member

What is Static Class in Java?

We as Java programmers have come across the keyword static and it is majorly seen with the main() function in Java. But does it mean that only functions can be declared static? The answer is No. In Java, variables, blocks, methods, and classes can be declared as static .

In order to declare a member as static, we use the keyword static before the member. When we declare a certain member as static, we do not have to create an instance of the class to access it.

Syntax

Here we have declared an Outer class and then declared a nested class.

Static classes in Java can be created only as nested classes. Let us understand the concept of inner, outer, and nested classes first.

1. Inner Class

Inner classes are the classes that are non-static and nested. They are written inside an outer class. We are unable to create an instance of the inner class without creating an instance of its given outer class. This is needed when the user has to create a class but doesn’t want other classes to access it. Here we can use an inner class for that reason.

2. Outer Class

Outer classes are the classes in which nested or inner classes are defined.

3. Nested Class

Nested class can be static or non-static. The non-static nested class is known as an inner class.

An instance of a static nested class can be created without the instance of the outer class. The static member of the outer class can be accessed only by the static nested class.

The following figure illustrates the inner, outer, and nested classes.

inner-outer-and-nested-class-of-static-nested-class

  • The nested static class can access the static variables.
  • Static nested classes do not have access to other members of the enclosing class which means it has no access to the instance variables (non-static variables) which are declared in the outer class.

Example

Explanation:

  • We have declared Outer and Nested classes. In the outer class, we have created a static member which can be accessed by the static class.
  • As we know, static members can be accessed by the static class only, so in the main() method, we have created an instance of the nested class without the instance of the outer class as our nested class is static.

Why We Use Static Class in Java?

In Java, the static keyword helps in efficient memory management. We can access the static members without creating an instance of a class. We can access them simply by using their class name. It is also a way of grouping classes together. Also, an instance of a static nested class can be created without the instance of the outer class.

Difference between Static and Non-static Nested Class

inner-class-accessing-static-and-non-static-member

  • Static nested class can access the static members of the outer class and not the non-static members, whereas non-static nested class, i.e., the inner class can access the static as well as the non-static members of the outer class.
  • We can create an instance of the static nested class without creating an instance of the outer class.

Conclusion

  • Static class in Java is a nested class and it doesn’t need the reference of the outer class.
  • Static class can access only the static members of its outer class.
  • Static class cannot access the non-static member of the outer classes.
  • Inner classes can access the static and the non-static members of the outer class.
  • We can create an instance of the static nested class without creating an instance of the outer class.
  • The static keyword helps in memory management.

See More

Источник

What is Static class in Java?

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.

Introduction

In Java, a class can be defined within another class. These types of classes are called nested classes or inner classes. The class in which the nested class is defined is known as the outer class.

Although an inner class can either be static or non-static, the static keyword in java can only be used for an inner class and not with an outer class. Hence, the concept of static classes simply provides a way of grouping classes together in Java.

Static classes are defined the same way as other inner classes in Java, the only difference being the static keyword behind the class name.

Syntax

The syntax for creating a static class is as follows:

The concept of static classes was introduced under the concept of inner classes, which are specially designed to handle some delicate functionality in a class.

A static class can access members, i.e., variables or methods of its outer-containing class.

Some attributes that differentiate a static class from a non-static inner class include:

  • A static inner class may be instantiated without instantiating its outer class, i.e., a static class does not need to create an instance of its outer containing class in order to create its own instance.
  • A static class can only access members of its outer containing class if the members are static in nature. This means that a static nested class does not have access to the instance variables and methods of the outer class that are non-static.

Example

An example of a static class implementation is as follows:

class Employee
private static String name = "Smith";
public static class Validator
public void testMethod()
System.out.println("This is a message from nested static class to " + name);
>
>
public static void main(String args[])
Employee.Validator nested = new Validator();
nested.testMethod();
>
>

The output of the above program is:

This is a message from nested static class to Smith

Here, the inner class was able to access the field of the outer class because it is also a static member. If the field is made a non-static variable, there will be a compiler error.

The static inner class ( Validator ) was also instantiated without having to instantiate its outer class ( Employee ), and its method was called successfully.

Importance of static class

The use of the static class is primarily for memory management. This is possible because static classes are only loaded by the class loader at the time of the first usage and not when its enclosing class gets loaded in JVM. Finally, it also helps when grouping classes together.

Источник

Оцените статью