Home java private static and private final static - why

private static and private final static – why

Author

Date

Category

In some cases it is necessary to mark the variables in this way? What is the use of the static , if it is labeled as private ?


Answer 1, Authority 100%

Look data template design singleton .

private static ensures a single instance of an object with properties in the stream, where the class is involved.

private static final will ensure that the copy is not substitute for something else.

This is useful when working with databases, or some resources that are not prone to separation.


Answer 2, authority 45%

What is the use of static, if it is marked as private?

It is necessary if you want to use a variable in static methods

public class CarFactoryFacade {
  private static CarFactory audiFactory = new AudiCarFactory ();
  private CarFactory bmwFactory = new BMWCarFactory ();
  public static Car createAudi () {
    return audiFactory.createCar ();
  }
  public static Car createBMW () {
    return bmwFactory.createCar (); & Lt; - error
  }

private final static – why

?

Final should be used, if the link is initialized only once and must be replaced. In most cases, static variable names and uses.

If the static variable is changed in the course of the program is likely to have any problems with the design (static – the absence of an object instance, in a sense, the absence of a state change links – change of state – & gt; contradiction)

.


Answer 3, Authority 5%

Access modifiers static final should be used when there is a need to explicitly specify that the variable is not whether or not allowed to change, ie modifiers static final is converted to a variable to a constant.


In turn, the variables marked with the modifier static are common variables with the only difference being that their instance (in the singular) is not stored with a copy of each object to be created (the memory area where the instance variables are stored), and in an object that describes your class at the same time on each JVM such an object exists in a single copy. With the variable declarations like static can do the same as with the usual variables in this case for access to such variables is unnecessary (but can be accessed and through an instance variable) to create an instance (of course if the variable is an access modifier public ), for example, create a class containing a static variable:

public class MyClass {
  public static int classVariable = 10;
}

Getting the outskirts of the static variable:

MyClass.classVariable;

Assign the new value of the static variable:

MyClass.classVariable = 100;

Such variables should be used when there is a need to have one variable for all instances of classes or if you want to use a variable in a static method (instance variables in this method is not available as the instances of a class may not even exist at the time of the static method call).

When using the modifier private variable properties “constants” are not changed, changes only the availability variable / constant.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions