Class attributes/fields are variables within a class.
The example creates a class called MyClass with two attributes: x and y :
|
|
The dot syntax (. ) can be used to access class attributes.
The example creates an object myObj of the MyClass class, and then uses the x attribute on the object to print its value:
|
|
The example sets the value of x to 4.
If you want a variable to always store the same value, like PI (3.141512...), declare the attribute as final :
final int x = 5;
|
|
For example, change the value of x to 4 in myObj2 , and leave x in myObj1 unchanged:
|
|
Other than that, they are as alike as chalk and cheese (nothing in common). |