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 6690921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:41:31+00:00 2026-05-26T05:41:31+00:00

This may seems a silly question for Java developers, however, I’m new to Java,

  • 0

This may seems a silly question for Java developers, however, I’m new to Java, and my background is from low level c.
I used to include an header file with all the constants that were relevant for my projects. (usually #define’s).
I’m working on a big Java project now, and there a few constants I need to make global (they fit in more than one class, and used in various parts of the project )

It makes it hard for me to decide where to put it, should I declare the same constant few times, one in each class ?

A lot of framework, uses XML files to declare constants & definitions for the framework (Hibernate, Log4J, etc.) Is it wise to use this kind of technique in my project ? if so, how can it be done easily ?

  • 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-05-26T05:41:32+00:00Added an answer on May 26, 2026 at 5:41 am

    As with many things, there are many ways to do it. One thing you should not do is declare them multiple times – that’s just plain silly. 😛

    Everything has to be in a class in Java, so either:

    1. Pick a “main” class (say I have a project called “FTPServerApp” – I could put them there)
    2. Create a “Util” class that contains all of them

    When you figure out where to put them, declare them all this way:

    public static final [type] [NAME_IN_ALL_CAPS] = [value];
    

    This will

    • make them available to all your project code, anywhere (public)
    • only one copy of the value exists across all instances of the class (static)
    • they cannot be changed (final).

    The ALL_CAPS_FOR_CONSTANT_NAMES, separated by underscores, is the convention in Java.

    So, if this was declared in a class called FTPServerAPP, and you had a constant called SERVICE_PORT it might be:

    public class FTPServerApp {
      public static final int SERVICE_PORT = 21;
    
      ...
    }
    

    …and you would access it, from any class, like this…

      FTPServerApp.SERVICE_PORT
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to rails so this may seem like a silly question but why
This may seem like a silly question but I can't figure it out. let's
This may seem a very silly question. Consider this: I have a simple Boolean
Now this may seem like a silly question, but I need to know how
I'm sorry if this seems silly, but I'm new to using Report Definition Language
I know this question may sound silly, but I am learning (at least trying)
This may seem like a silly question, but we are having an issue debugging
This may be a silly question, but nothing comes to mind straight away. Given
Ok, this may be a silly question, maybe my English knowledge, or just my
The question may at first sound silly, but maybe it isn't at all. Java

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.