Java is a pure object-oriented language. The class is the most important aspect of Java language. We define class to encapsulate functionality and data together in a well defined blueprint. This blueprint can be used later to create objects.
Where it comes from? Well, we often use the word classification. It is the action or process of classifying something according to shared qualities or characteristics. In the real world, you'll often find many individual objects all of the same kind. This is the core idea behind classes.
Class Anatomy
In this example we implement the class from previous diagram.
/* define a class */
public class Account{
// class public properties
public int act_number;
public String act_type;
// class internal field
private long act_amount;
//class constructor (same name ass class)
public Account(int p_number, String p_type) {
this.act_number = p_number;
this.act_type = p_type;
}
//fists class methods
public long withdraw(long p_amount) {
long result;
if (act_amount > p_amount) {
this.act_amount -= p_amount;
result = p_amount;
} else {
result = p_amount = this.act_amount;
this.act_amount = 0;
return result;
}
}
//second method
public void deposit(long p_amount) {
this.act_amount += p_amount;
}
//third method
public long closure() {
result = this.act_amount;
this.act_amount = 0;
return result;
}
//supplementary method
public void getAmount() {
return this.act_amount;
}
} //end of class
A class has several sub-programs defined inside class body that are called methods. One of these methods is very important and is distinguishing from the others. Is the constructor method that has its name identical with the class name: Account. This method is the one who instantiate objects by using "new" keyword.
Object called "this" represents current object. This is available inside the constructor method. You can use "this" as a prefix to all members, including private members. Also, "this" can be used inside any other method that is not static.
Properties are called attributes and sometimes fields. These can be private, or public. Private properties are encapsulated, visible inside the class methods but not accesible using dot operator. Public properties are available using object qualifier and dot operator.
Once you define a Class you can use this class like a data type to define Objects. The Objects are like variables but in Java we do not define variables we define "objects". To "instantiate" an object you must use "new" keyword. This allocate new memory for your new object and return an object handler.
Let's create a particular account using the class defined previously.
class AccountDemo {
public static void main(String[] args) {
Account myCheckAccount = new Account(2021,"checking");
//verify the account number
System.out.println("New account:" + Integer.toString(myCheckAccount.act_number));
//deposit some money
myCheckAccount.deposit(100);
//withdraw some money
myCheckAccount.withdraw(10);
//verify the amount left
System.out.println("New account:" + Integer.toString(myCheckAccount.getAmount()));
}
}
In Java we do not declare variables like in other languages. Instead we declare properties. A property do not hold a value until an object is instantiated. That means each property is related to an object. You can have multiple objects of the same class. That means one property can have multiple values, one value for each object.
A method is a group of statements that have a name, a list of parameters and a result type. There are methods that have no result. In this case we must specify type "void". So user knows the method is not a function and can't be used in expressions. Methods are declared and implemented inside class and are inherited by each object.
One method can return a result or not. If a method create a result it is also called function. Unfortunately Java is not a functional programming language, so in Java we do not have a "function" keyword to differentiate a method from a function declaration. Also a function is not an object.
Java is using "access modifiers" to hide & protect or to expose class members for external use. Public members can be accessed using dot operator notation: object_name.member_name. Here are the modifiers you can use in diverse member declarations to modify access level:
Java provide "non access modifiers" for other purposes:
Static members belong to class and not to class instances. This is a work around to the fact that Java is "pure" Object Oriented. You can't define anything outside of a class. So we use "static" modifier to make the class more usable without the need to instantiate an object from it.
In object-oriented programming, a "class variable" is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. So if we define a property of a Class and we use "static" keyword then the property become "class variable".
If we define a "static" method this method becomes "class method" and can be used outside of the class using dot operator. There is a catch though. In a static method we can't use any other member that is not "static". However, we can use a "static" member of a class using an object prefix.
Classes defined within another class are called nested classes. Nested classes can be static or non static. Non static classes are also called sometimes inner classes.
class OuterClass {
...
class NestedClass {
...
}
}
You can define a local class inside any block. For example, you can define a local class in a loop, or an if clause. Most common a local class is defined inside of a method body. Local classes have restrictions when accessing outer class members.
class OuterClass {
...
public void method() {
//define a local class
class localClass {
...
}
...
}
}
Anonymous classes are local classes declared and instantiatetd at the same time. You can use them only once, to instantiate a single object.
interface HelloWorld {
public void greet();
public void greetSomeone(String someone);
}
class Main {
public static void main(String[] args) {
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde";
public void greet() {
greetSomeone("tout le monde");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
frenchGreeting.greet();
frenchGreeting.greetSomeone("You");
}
}
See also: Oracle Documentation
Read next: Inheritance