A simple Java project demonstrating object-oriented programming concepts with classes and objects. This project includes a Student class implementation and UML diagrams to visualize the class structure.
This project demonstrates how to:
- Create and use Java classes
- Implement object instances
- Model real-world entities as code
- Visualize class structures with PlantUML
In Java, a class is a blueprint or template that defines the properties (attributes) and behaviors (methods) that objects of that class will have. An object is an instance of a class.
Think of a class as a cookie cutter, and objects as the cookies made from it - each cookie has the same shape but can have different decorations or flavors.
- Class: The template that defines attributes and methods
- Object: An instance of a class
- Attributes: Variables that store data about the object
- Methods: Functions that define the behavior of the object
- Constructor: Special method to initialize new objects
- Encapsulation: Hiding internal state through private variables and public methods
- Define a class:
public class Student {
private int age;
private String name;
// Constructor
public Student(int age, String name) {
this.age = age;
this.name = name;
}
// Methods
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}- Create objects (instances):
Student max = new Student(20, "Max Mustermann");
Student hannah = new Student(21, "Hannah Mustermann");- Access methods and properties:
max.setAge(22);
hannah.setName("Hannah Schmidt");PlantUML is an open-source tool that allows you to create UML diagrams from a simple text language.
-
Prerequisites:
- Java Runtime Environment (JRE)
- Graphviz (for some diagram types)
-
Installation options:
- Download standalone JAR from PlantUML website
- Install via package managers:
- macOS:
brew install plantuml - Linux:
apt-get install plantuml(Debian/Ubuntu)
- macOS:
- Use as a plugin in IDEs:
- IntelliJ IDEA (PlantUML integration plugin)
- Visual Studio Code (PlantUML extension)
- Eclipse (PlantUML plugin)
- Create a .puml file with your diagram definition:
@startuml Class_Example
class Student {
- age: int
- name: String
+ Student(int age, String name): void
+ setAge(int age): void
+ setName(String name): void
}
@enduml
-
Generate the diagram:
- Command line:
java -jar plantuml.jar file.puml - IDE: Use the plugin's preview or export functions
- Online: Use the PlantUML web server
- Command line:
-
PlantUML Syntax:
- Class members:
+public-private#protected
- Relationships:
--association<|--inheritance*--compositiono--aggregation
- Class members:
For more details, visit the PlantUML website.
Visual Studio Code offers powerful code formatting capabilities to keep your Java code clean and consistent.
-
Install Java Extension Pack:
- Open VS Code
- Go to Extensions (Ctrl+Shift+X or Cmd+Shift+X on macOS)
- Search for "Java Extension Pack" and install it
-
Configure Formatter Settings:
- Open Settings (Ctrl+, or Cmd+, on macOS)
- Search for "java format"
- Customize settings like tab size, indentation, and line wrapping
-
Format Options:
- Format current file: Right-click and select "Format Document" or use Shift+Alt+F (Windows/Linux) or Shift+Option+F (macOS)
- Format selection: Select code, right-click and choose "Format Selection" or use Ctrl+K Ctrl+F (Windows/Linux) or Cmd+K Cmd+F (macOS)
- Format on save: Enable "Editor: Format On Save" in Settings
-
Configure Java-specific formatting rules:
- Create a formatter profile XML file:
- Export from Eclipse or download a template
- In VS Code settings, set "java.format.settings.url" to the path of your XML file
- Create a formatter profile XML file:
-
Keyboard Shortcuts for Formatting:
- Format document: Shift+Alt+F (Windows/Linux) or Shift+Option+F (macOS)
- Format selection: Ctrl+K Ctrl+F (Windows/Linux) or Cmd+K Cmd+F (macOS)
Consistent formatting helps maintain code readability and makes collaboration easier across development teams.