Java has a lot of keywords that you can add to your functions and variables. These can be useful to make the code more readable, but also to better control what is and what can be done with certain variables. Sometimes they even allow the compiler to optimize your code and therefor make it faster.
Most keywords I describe here are put in front of constructs like functions, variables and class-definitions, but there are exceptions to this.
package
Remember imports? With package you can define how your class can be imported. If your class looks like this:
class Object … {…}
and you put a package definition in front of it(before all imports:
package xyz.abc;
Then your class can be imported as:
import xyz.abc.Object;
The disadvantage about packages is that you need to import a class if it is in a different package → this is good for bigger project to easily see which classes are used from where and to logically structure a project, but it also adds more lines that are not really needed in smaller projects.
public protected private "package private"
You can add the keywords public
, protected
and private
to variables, functions and classes. You can only add one or none of those keywords. If add none the variable automatically is "package private".
Those keywords restrict access to the variable/function/class:
keyword | access |
---|---|
private | can only be accessed from within the same class. |
protected | can only be accessed from within the same class or from subclasses. |
package private | can only be accessed by classes that are in the same package. |
public | can be accessed from everywhere. |
static
static
functions or variables are not instance-specific for every object, but are the same no matter from where you access them. static
functions and variables can be accessed without needing to have an instance of an object. Since they are not part of a specific instance, static functions cannot access non-static variables.
final
final variables cannot be changed after they were assigned once. They can be said to be constant.
volatile
When have two different threads and want a variable to be passed between those two threads as fast as possible, you should add the volatile
keyword in front of the variable. This ensures that the variable is passed to all other threads as soon as it changed.
abstract
It is not possible to create an instance of an abstract
class. Therefor abstract classes allow you to do some things that you cannot do with normal classes: an abstract
class can implement an interface without implementing all its functions → the functions need to be implemented in any non-abstract subclass of the abstract
class.
native
native
functions are function templates that are implemented in another programming language(usually to make a certain function faster than it would be possible in java).
I'm at the moment to busy to create some bigger example code. So here is just some random code from my quantum physics appthat shows a few of the keywords described:
Congratulations @quantumdeveloper! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit