i am a new to java . there is something new pops up called public final void. what does that do ? what is the difference between public static void and public final void? I would be greatly appreciated from guys!
4 Answers
public final void method() {}
This method is final
, and so can't be overrided in a subclass.
public static void method() {}
This method is static
, and so is class-scoped. You can't use class attribute in this method (unless if they are static
), and you call it using MyClass.method()
instead of anInstance.method()
.
Finally, void
is the return type of the function (meaning the method returns nothing) and public
is an access modifier.
Related questions:
That must be a part of method:
public - meaning it could be accessible by any other object
static - meaning it could be accessed by class name in addition to object as well.
void - meaning that method wont return any value. It will do some operations within the method.
final - Meaning you cant override the method in your sub class.

- 36,381
- 8
- 49
- 73
-
-
@rzysia - Well, the compiler (at least in an IDE like eclipse) will show a warning - *static methods should be accessed in a static way* :P – TheLostMind Mar 12 '15 at 09:10
-
1@TheLostMind - I believe ;) Quote from oracle tutorial: `Note: You can also refer to static fields with an object reference like myBike.numberOfBicycles but this is discouraged because it does not make it clear that they are class variables.` – rzysia Mar 12 '15 at 09:12
The final keyword is used for variables when they are constant, their value can be set only once, furthermore a final method cannot be subclassed.
static members belong to the class instead of a specific instance.

- 4,521
- 4
- 33
- 57
When you declare a method final
, subclasses of your class will not be able to override it.
When you declare a method static
you can call it without creating an object of class.

- 18,044
- 4
- 45
- 61