In this article, we'll explore the fundamental concept of declaring classes in Java, a key aspect of building robust and modular applications.
This article is a beginner-friendly guided on what is a class and how to declare it, if you want to investigate on the core java documentation about classes and objects, I highly recommend you to also check out the official java documentation
What is a class
A class (also called Local Class) in a context of Java is a template or a blueprint used to create objects, and define object properties (data types) and behaviours (methods).
In a real world a computer has properties, such as size, color, weight, brand. He also has behaviours such as playing music, playing videos, browsing and so on.
How to declare a class in Java
You can declare a class in the following way
class Computer {
// properties
String size;
String color;
int weight;
// Create a class constructor for the Computer class
public Computer() {
color = 'black'; // Set the initial value for the class attribute color
}
// methods
private void playVideo() {}
private void playMusic() {}
private void browseInternet() {}
}
This is a class declaration. The class body contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the properties that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.
special method
that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object (class) attributes.So, to use this class, we can instantiate it with the keyword new. Like the code below ↓
Computer apple = new Computer();
// Then the computer apple will be created will the default color = black
Nested Class
A Nested class is a class defined in another class. A nested class is a member of its enclosing class. Nested classes are divided into two categories:
- Non-static, also called Inner classes
- Static also called Static Nested classes
class OuterClass {
//...
class InnerClass {
//...
}
static class StaticNestedClass {
//...
}
}
Non-static nested classes (inner classes
) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.
As a member of the outer class
, a nested class can be declared private, public, protected or package private. (Recall that outer classes can only be declared public or package private.)
They are many reasons for using Nested Classes:
- You can group classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more cohesive.
- It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts below in the comments. Don't forget to subscribe to my newsletter to avoid missing my upcoming blog posts.
You can also find me here LinkedIn • Twitter • GitHub or Medium
Ending
In this article, we explored how to declare classes in Java. We learned that a class serves as a blueprint for creating objects and defines the properties and behaviors of those objects.
Additionally, we introduced the concept of nested classes, which can be either non-static inner classes or static nested classes.
By understanding how to declare classes in Java, you now have a solid foundation for creating object-oriented programs. With practice and further exploration, you can expand your knowledge and utilize the power of classes to build complex and efficient Java applications.
Remember to always refer to the Java documentation and explore additional resources to deepen your understanding of this topic.