var keyword in Java

  • We declare local variables in a method with its data type.
        







  • From Java 10 or higher version onwards , we can use var keyword to declare local variables in a method. Same variables can be declared with var keyword like below.









  • Java compiler takes the variables type from it's initializer. 20 is integer value, so age data type is int. 
  • You can use var keyword to declare reference variables.




  • You can use var to declare variable inside for loop, forEach loop.

 

  • You can use var keyword as variable name also.








Restrictions :

  • You can't use var to declare a variable without initializing it. You can't use with null variables.
    










  • You can't use var to declare multiple variables in single declaration.
    




  • You can't use var to declare array initializer .
    




  • You can't use var to declare class level instance , static variables.










  • You can't use var keyword to declare method parameters.






  • You can't use var as method return type.










Conclusion:

var keyword is good addition to Java programming language. It shortens the variables declaration. This is all for today. Happy Coding 😀




Sealed classes and Interfaces in Java


 

   


  • The above Test class is final . It means no other class can extend it. 

    
  • When we write a class in Java, any other class  can extend. We can't add a condition to allow only some classes can extend our class. 
  • We can now leverage the sealed classes feature to define list of classes which can extend our class.
  • We can declare sealed class with sealed keyword and  We need specify list sub classes using permits keyword.


  • Classes in permit clause must extend the sealed class otherwise we get compile time error.
  • Classes which are not in permit clause try to extend Test class, we get compile time error.
  • Sealed class's subclasses must be final or sealed or non-sealed.

  • Test classes defined Test1, Test 2 as its direct sub classes. 
  • We have declared Test1 as non-sealed . It means any number of classes can extend Test1. We no need to use final, sealed , non-sealed keywords in Test1 subclasses declarations. These are not subclasses for Test class.
  • We have declared Test2 as sealed. All sealed class rules are applicable here.  
  • We can't use non-sealed keyword for a class which doesn't extend any sealed class. 
  • Sealed classes and its sub classes must be in same module. if the sealed class is in unnamed module, its sub classes also should  be in same unnamed module. Sub classes can be in different package.

Sealed Interfaces:

  • Sealed interface rules are same like sealed classes.
  • We use sealed keyword to declare sealed interface.
  • We can specify list of classes which can implement sealed interface and list of interfaces which can extend sealed interface.
 
  • Classes and interfaces which are in permits clause only must implement or extend sealed interface . Other classes and interfaces should not implement or extend the sealed interface.
  • Implemented classes must be final or sealed or non-sealed. 
  • Sub Interfaces must be either sealed or non-sealed.
  • A class can extend a sealed class and implement a sealed interface.


Conclusion:
This feature is useful to define definite level of inheritance for our classes and interfaces. When our developing rest services , We can use this feature for interface to specify what all classes can implement it. It is mainly useful when we are developing some library or framework.


This is all about sealed classes and interfaces . If you have any questions, please comment below. Happy Coding 😀.


Different ways to run Spring boot App

 What are the different ways to run Spring boot app ?