- Attributes (also called fields, properties or instance variables) store the state of an object.
- Methods define the behaviour of an object.
public class Student {
// attributes
String name;
int marks;
// methods
void display() {
System.out.println(name + " scored " + marks);
}
String getGrade() {
if (marks >= 75) return "A";
else if (marks >= 65) return "B";
else if (marks >= 50) return "C";
else return "F";
}
}A method has a return type (void means it returns nothing), a name, a parameter list and a body.
Attributes in detail
An attribute is declared inside the class but outside every method. Every object gets its own copy, so two Student objects each have their own name and marks. If you do not give an attribute a value, Java gives it a default value:
| Attribute type | Default value |
|---|---|
int, long, short, byte | 0 |
double, float | 0.0 |
boolean | false |
char | '\u0000' (the null character) |
Any object type, such as String | null (no object) |
Local variables (variables declared inside a method) do not get default values. Using one before you give it a value is a compile error: variable total might not have been initialized.
Four kinds of variable
| Kind | Declared | Belongs to | Lives as long as |
|---|---|---|---|
| Instance variable (attribute) | In the class, outside methods | Each object | The object |
| Static variable | In the class, with static | The class (shared) | The program |
| Local variable | Inside a method | That method call | The method call |
| Parameter | In the method's brackets | That method call | The method call |
Methods in detail
String getGrade() // return type String, no parameters
void setMarks(int newMarks) // returns nothing, one parameter
double average(int a, int b, int c) // returns a double, three parameters- The return type says what kind of value the method sends back. A method with a return type other than
voidmust end every path with areturnstatement that gives a value of that type. - The parameters are the inputs that the method needs. The values you pass when you call it are called arguments: in
average(70, 80, 90),a,bandcare the parameters, and 70, 80 and 90 are the arguments. - The name and parameter list together are called the method's signature.
returnends the method immediately. That is whygetGrade()above can useif ... returnwithout nestedelseblocks.
Inside a method you can use the object's attributes directly. display() uses name and marks without receiving them as parameters, because they belong to the object the method was called on.
Getters and display methods: a method that returns a value (such as getGrade()) lets other code use the result, for example to count how many students got an A. A method that only prints (such as display()) is useful for output, but its result cannot be used in a calculation. Prefer methods that return values, and print in one place.
Remember
- Attributes hold the state of each object; methods define the behaviour shared by all objects of the class.
- Attributes get default values; local variables do not.
- Parameters are declared in the method; arguments are the values passed when calling it.