-3

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!

Latif
  • 117
  • 4
  • 16

4 Answers4

7
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:

Community
  • 1
  • 1
NiziL
  • 4,723
  • 20
  • 33
4

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.

SMA
  • 33,915
  • 6
  • 43
  • 65
  • static could be accesed by object name also – rzysia Mar 12 '15 at 09:08
  • @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
2

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.

moffeltje
  • 4,095
  • 4
  • 24
  • 49
0

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.

antonio
  • 17,130
  • 4
  • 43
  • 56