Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8506055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:29:26+00:00 2026-06-11T02:29:26+00:00

I have a class with has some miniclasses within it, which act as storage

  • 0

I have a class with has some miniclasses within it, which act as “storage units” for constants. The problem is, I need some information to assign values to these constants, and I only get that information in the constructor. Unfortunately, the miniclass is not declared in the constructor, and NetBeans tells me that I can’t assign to a final variable, even if it’s only been declared, not initialized. How should I go about this?

Edit: An example of some code that would have this problem:

public class Car {
    class constants {
        public final String MAKE;
        public final String REGISTRATION_NUMBER;
    }
    public Car(String make, String regNumber) {
        constants.MAKE = make;
        constants.REGISTRATION_NUMBER = regNumber;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:29:28+00:00Added an answer on June 11, 2026 at 2:29 am

    What you are trying to do can’t work as you need an instance of your Constants class somewhere. One way would be to do as below, but then there is nothing constant about that constant…

    public class Car {
        class Constants {
            public final String MAKE;
            public final String REGISTRATION_NUMBER;
            private Constants(String make, String regNumber) {
                this.MAKE = make;
                this.REGISTRATION_NUMBER = regNumber;
            }
        }
    
        public final Constants constants;
        public Car(String make, String regNumber) {
            this.constants = new Constants(make, regNumber);
        }
    }
    

    You can then call:

    Car car = new Car("abc", "def");
    String make = car.constants.MAKE;
    

    Note: a probably better (and simpler) design would be

    public class Car {
        private final String make;
        private final String regNumber;
    
        public Car(String make, String regNumber) {
            this.make = make;
            this.regNumber = regNumber;
        }
    
        //getters
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ class implementation file which has some constants. The constants appear
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have a class Book which has some annotations like @Override , @Before ,
Suppose I have some class which has a property actor_ of type Actor .
If you have a class that has some named constants, what is the best
I have a Java class which has some field annotated with @SearchCriteria(criteria = class1.class2.field)
I have created a class say A which has some functions defined as protected.
I have a class which has some unit tests, but when I am running
I have a class which has some properties of type List<float> , List<int> etc.
I have a .as Class which has some tests. Now I am wondering is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.