Skip to main content

Static and final, when and how to use

Usually when some one ask a simple question when you will make variable or function as static, a simple answer comes to mind

  •  Variables : when i want to make single instance of that variable shared across all objects of this class. Good answer :) 
  • Functions : When i want to access a function of class without creating its object. Good answer :) 
  • Currently not talking about classes 
Both are good answer but not complete. There are more things static do behind the screen then things mentioned above.

Static fields will not be serialised.

Check Following code


Class ClassWithStaticFunction{

public void doSomething(){
...
    doSomethingMore();
...
}
private void doSomethingMore(){
...
...

}

}

In above example a developer can say i can't make it better performance unless you tell me what these functions are doing(... lines), but that's incorrect you can make it performant without knowing about what goes inside it.

 When a function is made static as a developer you have made sure that binding will be done at compile time, but that's not 100% true either. It will depend on the caller.

What happens if you make doSomethingMore function as static
If you just make this function as static and no other change, you have done good job but you left the important part. You need to make sure when you call this function from doSomething it should be called like this
ClassWithStaticFunction.doSomethingMore();

Comments

  1. It is really a helpful blog to find some different source to add my knowledge.
    Switch to NBN

    ReplyDelete

Post a Comment

Popular posts from this blog

How to add custom user attributes in keycloak and access them in spring boot application

Sometime it may be possible you want to add more parameters to standard registration page of keyloak for your users and aaccess that data in your spring boot application. This artical will show step by steps on how to add such extra attributes. What is Keycloak Keycloak is an open source software product to allow single sign-on with Identity Management and Access Management aimed at modern applications and services, to learn more visit  h ttps://www.keycloak.org/ What is Spring boot Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". To learn more visit  https://spring.io/projects/spring-boot To add an extra attribute in keyclkoak server you will need to edit actuall html template and then registaer new attribute in json response so that it will be available on client. Edit HTML template Lets assume we want to add mobile number on default registration page. Go to Keycloak home installation directory edit fil

How to create java maven project in intelij

Open intellij Create a new java maven project in intellij . Select Maven type, Select JDK you want to use and click Next. Enter GroupId and ArtifactId, click Next Select project Location and click finish Intelij will display a warning, just press Ok. Once project is created , a popup may appear asking for auto import, select "Enable Auto Import" You will have a project created, looking something like this Note: I have created these instructions using, Instruction for other OS or intellij Version shouldn't be much different, if you need instruction for other version leave a comment and i will try to come up with another set of instructions. Intellij 2017.2.1 Java 1.8 Mac OS