Buddy, I know it’s been years, but other people might have the same question.
Our friend Zignd’s answer is the best, most correct, but difficult to understand.
Your definition of Static is wrong. But why do you think that?
Reason: in C a Static FUNCTION is visible only to its source file. In Java a Static method means something else.
In Java, a Static method means that it is UNIQUE to all its Objects. It always does the same thing.
Therefore, to call a Static method it is not necessary to instantiate an object.
Example:
.
public class Main
{
public static void main(String[] args)
{
ClasseB objB;
objB = new ClasseB(); // Não precisa instanciar objB para usar getValor
System.out.println("Ola\n");
//
System.out.println("Valor= " + ClasseB.getValor()); // Funcionaria mesmo sem instanciar objB, pois está chamando o metodo static.
System.out.println("Valor= " + objB.getValor()); // Está chamando o objB, logo só funciona se instanciar obj. (objB = new ClasseB();)
System.out.println("Valor= " + ClasseB.getValor());
}
}
class ClasseB {
private static int valor = 0;
public static int getValor()
{
valor ++;
return valor;
}
public ClasseB()
{
}
}
Exit:
MEU@PC:~/tmp$
MEU@PC:~/tmp$ javac Main.java
MEU@PC:~/tmp$ java Main
Ola
Valor= 1
Valor= 2
Valor= 3
System.out.println is a Static method, so you do not need the instance to use. (Make System system system = new System());
- Static variables are similar in C and Java.
Think of them as GLOBAL variables for all objects, but they are only accessible within the scope where it was created. In the case of java, within its Class (or object) and global between the objects.
What do you mean? When calling the program, the Static variable is created. It will remain in memory until the program is finished.
But it is inaccessible outside her scope (of the keys where it was created {}).
That is why it is necessary
public class Main
{
public static void main(String[] args)
{
ClasseA objA = new ClasseA();
ClasseA objB = new ClasseA();
System.out.println("System.out.print nao precisa de NEW por ser static.\n");
System.out.println("ClasseA.valorStatico= " + ClasseA.getValorStatico());
System.out.println("objA.valorStatico= " + objA.getValorStatico());
System.out.println("ClasseA.valorStatico= " + ClasseA.getValorStatico());
// System.out.println("objA.valorStatico= " + ClasseA.getValorLocal()); <- Erro de compilacao, método não é estatico.
System.out.println("objA.valorLocal= " + objA.getValorGlobalLocal());
System.out.println("objA.valorLocal= " + objA.getValorGlobalLocal());
System.out.println("objA.valorStatico= " + objA.getValorStatico());
// PARA B
System.out.println("ClasseA.valorStatico= " + ClasseA.getValorStatico());
System.out.println("objB.valorStatico= " + objA.getValorStatico());
System.out.println("ClasseA.valorStatico= " + ClasseA.getValorStatico());
// System.out.println("objB.valorStatico= " + ClasseB.getValorLocal()); <- Erro de compilacao, método não é estatico.
System.out.println("objB.valorLocal= " + objA.getValorGlobalLocal());
System.out.println("objB.valorLocal= " + objA.getValorGlobalLocal());
System.out.println("objB.valorStatico= " + objB.getValorStatico());
}
}
class ClasseA {
private static int valorStatico = 0;
private int valorGlobalLocal =0;
public static int getValorStatico()
{
return valorStatico++;
}
public int getValorGlobalLocal()
{
return valorGlobalLocal++;
}
public ClasseA()
{
}
}
So we’ll have the next exit:
java Main
System.out.print nao precisa de NEW por ser static.
ClasseA.valorStatico= 0
objA.valorStatico= 1
ClasseA.valorStatico= 2
objA.valorLocal= 0
objA.valorLocal= 1
objA.valorStatico= 3
ClasseA.valorStatico= 4
objB.valorStatico= 5
ClasseA.valorStatico= 6
objB.valorLocal= 2
objB.valorLocal= 3
objB.valorStatico= 7
Note, Classea.valorStatico is incremented either when it is called by objA, objB or Classeb.
In my understanding, the simple fact that you make the variable instance as Static, He’s already a Singleton.
These examples of Singleton codes are examples for various languages. In Java
only instantiate as var as Static, if var = null, then you need to create the connection with DB.
Now, the private Conexao() (private in Constructor), makes your class never instantiated by another class, only by itself. Therefore, only one Static method (from the Connected class) can allocate the object. This serves to have full control over how to create the object.
If in my class the constructor were private:
classe ClasseB
{
...
private ClasseB()
{
}
}
It would never be possible to instantiate externally B:
ClasseB varB = new ClasseB() // daria erro
Got it @Zignd, thank you so much for the explanation! Now I realized that my vision on Static is completely wrong, I need to study more about it.
– Duds
You’re welcome, at least I was able to help you. (^-^)
– Zignd
Another question, but what is the point of assigning an access modifier, such as private, to a Static member? Where private access would limit access to a Static member?
– Duds
@Duds the advantage of doing this is that you have control of how to offer this static instance. You may for example want to perform some operations in that instance before passing it on to the person who requested it. To do this you can create a property that only owns the
get
and modify the instance as you want inside the blockget
before returning it.– Zignd