What is the meaning of an attribute being private and static at the same time in a class?

13

I'm studying about design pattern singleton , and in a code snippet in java , I came across a situation where I was in doubt. Here is the code snippet below:

public class Conexao {

    private static Conexao instance = new Conexao();

    public static Conexao getInstance() {
        return instance;
    }

    private Conexao() {
    }

    public Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://endereco_do_banco.ws", "login", "senha");
        return conn;
    }

}

My question is in the private static Conexao instance = new Conexao(); excerpt, what's the point of creating a private and static attribute at the same time? If we assign it as private is because we want it not to be visible to other classes, and putting it as static , we would be making it visible to other classes, is not it contradictory?

    
asked by anonymous 21.11.2015 / 16:26

3 answers

9
  

If we assign it as private is because we want it not to be visible to other classes, and putting it as static, we would be making it visible to other classes, is not this contradictory?

Although your definition of the private access modifier is correct, you have made a mistake in setting the static modifier (I'll explain next what it actually does) and what you said about it would only fit the modifier % access. You probably got confused in the definitions.

The public modifier causes a member declared with it to be accessible through the type where it was declared and not through an instance of the type. That is, a property or method declared as static can be accessed directly without the need for you to instantiate the class where it was declared ( static or Classe1.Propriedade1 = "Oi"; ).

Note that when it is necessary to create an instance of the class when the member does not have the Classe1.Metodo1(); ( static ) modifier.

The access modifier var obj = new Classe1(); obj.Propriedade1 = "Oi"; obj.Metodo(); does, however, leaves the members of a type (class) or a type in itself, accessible without any restriction.

I recommend that you read the public switch to understand it better, and also in setting the access modifier static .

  

What is the point of creating a public and private attribute at the same time?

I do not think it's necessary to answer this first question, but come on.

The answer is simple, the Singleton design pattern requires it to be ugly in this way.

As described in the Wikipedia article, to implement the standard the class must have the following characteristics:

  • A private static field needs to be created in the class to store the only instance of the class that will be distributed by the application;
  • A public and static method that will return the instance of the class that is stored in the field defined in the previous point;
  • Instances of the class can only be created within itself through the method defined in the previous point, so it is necessary for the class to have a private constructor.

But note that setting fields like static is not restricted to this pattern, you may come across situations within a project that might benefit from a field declared in this way.

    
21.11.2015 / 16:41
5

It's not because your definition is wrong. The understanding of what private does is correct.

But static does not say that it is visible to other classes. It says that the member belongs to the class and not to an instance of the class. It has nothing to do with visibility, it has to do with the property of the given, with the location where it will be stored and therefore through which component of the language it will be accessed. It is a scope modifier. It defines the lifetime of the member.

As members of the instance can be public or private, members of the class can also.

A static field is one that is available in a class that is unique to the entire application. It is not bound to an instance of the class. It belongs to the class itself and is shared by all instances of this class created during the execution of the application. Life time is any application. While the lifetime of an instance member is the same as the lifetime instance instance to which it belongs.

A static field exists within the class, but its visibility is defined by another modifier.

A private field is one that can only be viewed / accessed within the class. Everything that is private is implementation detail. It means you do not want anyone to know how you are internally (not in the sense of being an industrial secret), giving you the chance to change when you want, any way you want.

What is public is part of the API, is something that you commit to keep stable, after all anyone can access it. You have no control who accessed it and in what form.

Scope and visibility are different things. The place of the existence of the die is the scope. Where there may be a request for access to this data, ie where it can be accessed, it is visibility.

And it's simple to test and verify this since you have two static members each with different visibility. Both are variables belonging to the class and not to an instance. getConnection() is instance.

Try to access the instance field from another location outside this class. You can do it any way you want (except for reflection or another trick that goes over the language :)). It does not. Only access within the class.

On the other hand the getInstance() method is static and public. Try to access from outside. You can.

That's all.

    
21.11.2015 / 16:38
2

The reasons for private static are:

  • private because you do not want it to be visible outside the class.

21.11.2015 / 18:09