0
Static Void - Java
Could someone please give me a simple explanation of "static" and "void"? By simple, think of Michael Scott asking Oscar to describe what a surplus is.
3 Respostas
+ 16
Void:
We can use void keyword only with method and it tells the compiler this method doesn't return any value.
Static:
The static keyword is can be used with variable, method, block, and class. It is used to manage the memory in java. The static keyword always belongs to class instead of objects.
For more details: https://javagoal.com/static-keyword-in-java/
+ 2
static is a modifier that indicates that a function or variable belongs to the class and not the object.
Example:
public class Foo {
public int a = 9;
public static int b = 10;
}
Foo bar = new Foo();
bar.a; //Works because a is public
Foo.a; //Does not work. a belongs to the object not the class
Foo.b; //Works because b is static
==================
void indicates that a function does not return anything.
https://www.sololearn.com/learn/Java/2153/
- 1
If you know python then it is easy to understand!
static - doesn't require an instance of the class we can call it using its class name followed by its name with a dot (.) in between! If we want to call a static method in the same class we don't even require the class name!
(Advantage: no instantiation required
Disadvantage: it has no relation with its class, it cannot use any other non-static methods or variables in it)
void - when function is not returning it returns void which means nothing! Interesting thing: constructor doesn't return anything still there is no void before it!