Skip to main content

Posts

Showing posts with the label Static keyword

Static keyword Interview Question in Java

Static Keyword Interview Question in Java 1. In Java, why do we use static variable? Whenever we want to have a common property for every objects of a class, we use a class level variable i.e. a static variable. This type of   variable is loaded in memory only once at the time of class loading. So it saves memory, since it is not defined per object in Java. 2. Why it is not a good practice to create static variable in Java? Static variables are common to all the object of a class. If a new object is create, there is no need to test of static variable. Any code that uses static variable can be in any state. It can be within a new object or at a class level. So the scope of static variable is open ended in a Java class. If we want tighter control on scope, then variables should be created at the object creation level. Also defining static variables is not a good practice because they go against the principles of Object Oriented Programming. 3. What is t...