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
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.
private method: Cannot be accessed outside the class (or possibly "compilation unit"--have to check the JLS) where it's declared.
private variable: Cannot be accessed outside the class (or possibly "compilation unit"--have to check the JLS) where it's declared.
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.