Concept Behind Making A Class Static Final in Java

static class java
static final variable in java with example
static in java
static method java
final keyword in java
static object in java
final class in java
static block in java

I am new to Java , and at school level, i have one query can any one better explain me the concept behind it , what it's means and when to use and why .

public class ParentClass {
    private static final ParentClass ourInstance=new ParentClass();
    private ArrayList<Integer> arr=new ArrayList<>();
    public static ParentClass getinstance() {
        return ourInstance;
    }
    private ParentClass() {

    }
    public void setList(ArrayList<Integer> al) {
        arr.addAll(al);
    }

    public ArrayList<Integer>  getList(){
        return arr;
    }

}

I want to focus on this line private static final ParentClass ourInstance=new ParentClass(); what it's means and when we have to declare the syntax like this ? I know we can make the class Final then no subclass can extends the final class.In the Above code the way of declaration is something different ,that's waht confused me .


private static final ParentClass ourInstance=new ParentClass();

When you declare field of a class as final then you can't reassign its value after initialisation.

What it means is you cannot execute below code:

ParentClass otherInstance = new ParentClass();
ourInstance = otherInstance;   // Not allowed as ourInstance is final.

But you can perform below operations i.e. you can change fields of ourInstance by calling its setter methods: - e.g.

ourInstance.setSomeMethod(someValue);   // Allowed as you are not modifying object's reference but only its properties which are not final.

Static class in Java, Only static members of Outer class. // is directly accessible in nested. // static class. public void printMessage(). {. // Try making 'message' a non-static. // variable  For non static methods if we declare it as final then we are preventing that method from overriding so it can not be overridden in sub class. When we declare static method as final its prevents from method hiding. When we declare final static method and override in sub class then compiler shows an error


What you are doing is "You are creating Class constant", the word final in this context is used to create constant. And also its static, so there is no need to create an instance to access it.

You are instantiating it where its declared, because you cannot do it outside since your constructor is private. So accessing the instance of this class only possible by calling getInstance, that too already created instance is accessible. Outside its not possible to create instance. That way you are creating singleton, means only one instance of this class will be created in VM.

Final static variable in Java, Declaring variables only as static can lead to change in their values by one or more instances of a class in which it is declared. Declaring them as static final will​  Key Differences Between Static and Final in Java. The static keyword is applicable to a nested static class, variables, methods and blocks. On the other hand, final keyword is applicable to class methods and variables. Static variable can be initialized any time whereas, a final variable must be initialized at the time of declaration.


I know when we can make the class Final then no subclass can extends the final class.

True. But in your code, the final is applying to a field, which means that this static field ourInstance will not be modified, i.e., it will not be re-assigned to another instance.

The reason why static is applied here:

private static final ParentClass ourInstance = new ParentClass();

is working with

public static ParentClass getinstance() {
    return ourInstance;
}

private ParentClass() {
}

to implement the singleton design pattern.

Understanding Class Members (The Java™ Tutorials > Learning the , Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class. The Java programming language supports static methods as well as static variables. Static methods  The answer is Yes, some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block and Static Classes Java allows a class to be defined within another class. These are called Nested Classes.


Final static variables in Java, There would only be one copy of each class variable per class, regardless of how many objects are created from it. Static variables are normally  I am new to Java , and at school level, i have one query can any one better explain me the concept behind it , what it's means and when to use and why . public class ParentClass { private static


Java, 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. The static keyword is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables.


Can we declare a class Static in Java?, If you are familiar with concept of static method and static variable in Java, then you know that anything static can be accessed without creating  The basic difference between static and final in java is that static is a keyword in java that is used to define the class member that can be used independently of any object of class whereas final keyword in java is used to declare a constant variable that cannot be overridden and a class that cannot be inherited.