Java Fields
Java Fields
A field in Java is a variable inside a class. For instance, in a class representing an employee, the
Employee
class might contain the following fields:- name
- position
- salary
- hiredDate
The corresponding Java class could be defined like this:
public class Employee { String name ; String position ; int salary ; Date hiredDate; }
Field Declaration Syntax
A field is declared using the following syntax:
[access_modifier] [static] [final] type name [= initial value] ;
The square brackets [ ] around some of the keywords mean that this option is optional. Only type and name are required.
First an access modifier can be declared on a field. The access modifier determines which object classes that can access the field. In the
Employee
example above there were no access modifiers.
Second, a data type for the field must be assigned. In the
Employee
example above the data typesString
, int
and Date
were used.
Third, the field can be declared
static
. A static field is a field only accessible by static
methods. Static fields belongs to the class, not instances of the class. Thus, all instances will see the same field value. A non-static field value can be different for every object (instance of a class).
Fourth, the field can be declared
final
or not. A final
field cannot have its value changed. A final field must have an initial value assigned to it, and once set, the value cannot be changed again.
Fifth, the field is given a name. You can choose this name freely, but there are some restrictions on what characters the name can contain.
Sixth, you can optionally set an initial value for the field.
Some of the above options are described in more detail in the following sections.
Access Modifiers
The access modifiers of a field determines whether the field can be accessed by classes outside the class owning the field. There are four possible access modifiers:
- private
- package
- protected
- public
The
private
access modifier means that only code inside the class itself can access the field.
The
package
access modifier means that only code inside the class itself, or other classes in the same package, can access the field. You don't actually write the package modifier. By leaving out any access modifier, the access modifier defaults to package scope.
The
protected
access modifier is like the package
modifier, except subclasses of the class can also access the field, even if the subclass is not located in the same package.
The
public
access modifier means that the field can be accessed by all classes in your application.
Here are a few examples of fields declared with access modifiers. The modifiers are in bold.
public class Customer { private String email; String position; //no modifier = package access modifier protected String name; public String city; }
The above use of modifiers are for the sake of this example only. You would probably not use all access modifiers in the same class. Most often you use
private
and protected
. For simple, data carrying classes you may declare all fields public
.Static and Non-static Fields
A field can be static or non-static.
A static field belongs to the class. Thus, no matter how many objects you create of that class, there will only exist one field located in the class, and the value of that field is the same, no matter from which object it is accessed. Here is a diagram illustrating static fields:
Static fields are located in the class, not in the instances of the class. |
You define a static field by using the
static
keyword in the field declaration, like this:public class Customer { static String staticField1; }
Static fields are located in the class, so you don't need an instance of the class to access static fields. You just write the class name in front, like this:
Customer.staticField1 = "value"; System.out.println(Customer.staticField1);
Non-static fields, on the other hand, are located in the instances of the class. Each instance of the class can have its own values for these fields. Here is a diagram illustrating non-static fields:
Non-static fields are located in the instances of the class. |
You define a non-static field simply by leaving out the
static
keyword. Here is an example:public class Customer { String field1; }
To access a non-static field you need an instance of the class (an object) on which you can access it. Here is an example:
Customer customer = new Customer(); customer.field1 = "value"; System.out.println(customer.field1);
Final Fields
A field can be declared
final
. A final
field cannot have its value changed, once assigned. You declare a field to be final
by adding the final
keyword to the field declaration. Here is an example:public class Customer { final String field1 = "Fixed Value"; }
The value of the
field1
field cannot be changed now. That means, that even if the field belongs to objects (class instances), you cannot vary the value of the field from object to object.
When you cannot change the value of a final field anyways, in many cases it makes sense to also declare it
static
. That way it only exists in the class, not in every object too. Here is an example:public class Customer { static final String field1 = "Fixed Value"; }
Since
static final
fields are often used as constants, the naming conventionn is typically to write the field name in all uppercase, and to separate the words with underscore _ . Here is an example:public class Customer { static final String CONSTANT_1 = "Fixed Value"; }
Naming Fields
The name of a field is used to refer to that field from your code. Here is an example:
Customer customer = new Customer(); customer.city = "New York"; System.out.println(customer.city);
The first line creates a new
Customer
object (an instance of the Customer
class), and stores it in a variable called customer
. The second line assigns the String
value New York
to the Customer
objects city
field. The third line prints out the value of the city
field to the console output.
The naming restrictions and naming conventions for fields are the same as for any other type of variable.
Initial Field Value
A field can have be given an initial value. This value is assigned to the field when the field is created in the JVM. Static fields are created when the class is loaded. A class is loaded the first time it is referenced in your program. Non-static fields are created when the object owning them are created.
Here is an example of a field being declared with an initial value:
public class Customer { String customerType = "OnlineCustomer"; }
Whether you want to initialize your variables with an initial value is up to you. I have made it a habbit to always initialize my variables to some sensible value, but it is just a habbit. It is not necessary to do so.
0 comments: