GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing jenkov.com — Java Fields Java Language
Java Fields
A Java field is a variable inside a class. For instance, in a class representing an employee,
the
The corresponding Java class could be defined like this: public class Employee {
String name ;
String position ;
int salary ;
Date hiredDate;
}
Field Declaration SyntaxA Java 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 for a Java field. The access modifier determines which object
classes that can access the field. In the
Second, a data type for the Java field must be assigned. In the
Third, the Java field can be declared
Fourth, the Java field can be declared Fifth, the Java 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. Java Field Access ModifiersThe Java field access modifier determines whether the field can be accessed by classes other than the the class owning the field. There are four possible access modifiers for Java fields:
The
The
The
The 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 Java field access 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 The Java access modifiers are covered in more detail in my Java access modifiers tutorial. Static and Non-static FieldsA Java 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:
You define a static field by using the 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 Java 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:
You define a non-static Java field simply by leaving out the 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 Java field can be declared public class Customer {
final String field1 = "Fixed Value";
}
The value of the
When you cannot change the value of a final field anyways, in many cases it makes sense to also declare
it public class Customer {
static final String field1 = "Fixed Value";
}
Since public class Customer {
static final String CONSTANT_1 = "Fixed Value";
}
Naming Java FieldsThe name of a Java 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 The naming restrictions and naming conventions for fields are the same as for any other type of variable. Initial Field ValueA Java 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 Java field being declared with an initial value: public class Customer {
String customerType = "OnlineCustomer";
}
Whether you want to initialize your Java fields (and other variables) to an initial value is up to you. I have made it a habit to always initialize my variables to some sensible value, but it is just a habit. It is not necessary to do so. Next: Java Methods
Close TOC
All Trails Trail TOC Page TOC Previous Next | ||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||||||