Java Reference
In-Depth Information
Here, <<modifiers>> are keywords that associate special meanings to the class declaration. A class declaration
may have zero or more modifiers. The keyword class is used to declare a class. The <<class name>> is a user-defined
name of the class, which should be a valid identifier. Each class has a body, which is specified inside a pair of braces
( {} ). The body of a class contains its different components, for example, fields, methods, etc. The following snippet of
code defines a class named Human with an empty body. Note that here the Human class does not use any modifiers.
// Human.java
class Human {
// Empty body for now
}
Declaring Fields in a Class
Fields of a class represent properties (also called attributes) of objects of that class. Suppose every object of human
class has two properties: a name and a gender. The human class should include declarations of two fields: one to
represent name and one to represent gender.
The fields are declared inside the body of the class. The general syntax to declare a field in a class is
<<modifiers>> class <<class name>> {
// A field declaration
<<modifiers>> <<data type>> <<field name>> = <<initial value>>;
}
A field declaration can use zero or more modifiers. The data type of the field precedes its name. Optionally, you
can also initialize each field with a value. If you do not want to initialize a field, its declaration should end with a
semicolon after its name.
With the declaration of two fields, name and gender , the declaration of the Human class will look as shown:
// Human.java
class Human {
String name;
String gender;
}
It is a convention (not a rule or a requirement) in java to start a class name with an uppercase letter and capitalize
the subsequent words, for example, Human , Table , ColorMonitor , etc. the name of fields and methods should start with a
lowercase letter and the subsequent words should be capitalized, for example, name , firstName , maxDebitAmount , etc.
Tip
The Human class declares two fields: name and gender . Both fields are of the String type. Every instance (or object)
of the Human class will have a copy of these two fields.
Sometimes a property belongs to the class itself, not to any particular instance of that class. For example, the
count of all humans is not a property of any specific human. Rather, it belongs to the human class itself. The existence
of the count of human is not tied to any specific instance of the human class, even though each instance of the human
class contributes to the value of the count property. Only one copy of the class property exists irrespective of the
number of instances that exists for the class. However, a separate copy of the instance property exists for each instance
of a class. For example, a separate copy of the name and the gender properties exist for each instance of the Human class.
You always specify name and gender of a human. However, even if there is no instance of the Human class, you can say
that the count of the Human class instances is zero.
 
 
Search WWH ::




Custom Search