private static final object

Hi
I am learning Java . In the book that I am reading is said about private and static fields and methods . But in a example code I saw a stange code :

private static final Random randomNumbers = new Random();

randomNumbers is an object of class Random

I am confused . Are there private and static objects ?? and if there are , What they mean ?? What is the meaning of a private or static object ??
I know the meaning of private or static fields and methods but I don't know the meaning of private and static objects .
Please help me...Thanks

Matthew Brown

Bartender

Posts: 4568

9

posted 4 years ago

1

There's no difference. In that example randomNumbers is a private static field. So if you understand private and static fields you already understand it. In this case, the field holds a reference to a Random object, but if it held an int or a reference to a String the principle would be the same.

Objects do not have the property of being public or private, or static or non-static, final or non-final. Those concepts simply don't apply to objects. What you're seeing is a private static final variable.

static method: Associated with the class as a whole, not with any particular instance. No "this" during execution.
static variable: Associated with the class as a whole, not with any particular instance. No associated "this".

final method: Cannot be overridden.
final variable: Cannot have its value changed after initialization.