Skip to content

YuriiJavaDev/JavaBasics_Task_263_V0.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

User Profile: Shared Instance Initialization (JavaBasics_Task_263_V0.1)

📖 Description

In systems with multiple ways to create an object (overloaded constructors), maintaining consistent initial state can be challenging. This project demonstrates how a non-static initialization block serves as a common entry point for all constructors. Regardless of whether a user chooses "Fast Registration" or "Full Profile," the system automatically assigns a default identifier before the specific constructor logic executes. This pattern promotes the "Don't Repeat Yourself" (DRY) principle and ensures mandatory setup tasks are never skipped.

📋 Requirements Compliance

  • Non-static Block: Implemented a shared block to assign a default userIdentifier.
  • Constructor Overloading: Created two distinct constructors (no-arg and parameterized) for different registration flows.
  • Execution Order: Verified that the initialization block runs before every constructor call.
  • Encapsulation: Protected the userIdentifier field while ensuring it is initialized for every instance.

🚀 Architectural Stack

  • Java 8+ (Instance Initialization Blocks, Constructor Overloading)

🏗️ Implementation Details

  • UserProfile: The core model managing user identity and profile names.
  • ProfileApp: The application driver simulating different registration scenarios.

📋 Expected result

Temporary user ID assigned.
Profile created without a name.
Temporary user ID assigned.
Profile created with name: Daria

💻 Code Example

Project Structure:

JavaBasics_Task_263/
├── src/
│   └── com/yurii/pavlenko/
│                  ├── UserProfile.java
│                  └── ProfileApp.java
└── README.md

Code

package com.yurii.pavlenko;

public class ProfileApp {

    public static void main(String[] args) {

        UserProfile fastRegistration = new UserProfile();
        UserProfile fullRegistration = new UserProfile("Daria");
    }
}
package com.yurii.pavlenko;

public class UserProfile {
    private String userIdentifier;
    private String userName;

    {
        this.userIdentifier = "DEFAULT_REG_ID";
        System.out.println("Temporary user ID assigned.");
    }

    public UserProfile() {
        System.out.println("Profile created without a name.");
    }

    public UserProfile(String initialName) {
        this.userName = initialName;
        System.out.println("Profile created with name: " + initialName);
    }
}

⚖️ License

This project is licensed under the MIT License.

Copyright (c) 2026 Yurii Pavlenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...

License: MIT

About

This is a tutorial project. JavaBasics_Task_263_V0.1 User Profile Registration: demonstrating shared instance initialization across multiple constructors using non-static blocks to ensure data consistency. 210326_1034

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages