In Java, a class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors. Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object.
Declaring Java Technology Classes
Basic syntax of a Java class:
<modifiers> class <class_name> {
[<attribute_declarations>]
[<constructor_declarations>]
[<method_declarations>]
}Methods are nothing but members of a class that provide a service for an object or perform some business logic. Java fields and member functions names are case sensitive. Current states of a class’s corresponding object are stored in the object’s instance variables. Methods define the operations that can be performed in java programming.
Basic syntax of an attribute:
<modifiers> <type> <name>;
Basic syntax of a method:
<modifiers> <return_type> <name>
([<argument_list>]) {
[<statements>]
}
Examples:
public class Dog {
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int newWeight) {
weight = newWeight;
}
}
Example of class definition
public class Foo {
private int x;
private float y = 10000.0F;
private String name = "Motel";
}