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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:33:33+00:00 2026-05-31T19:33:33+00:00

In c\c++ can define: #ifndef <token> /* code */ #else /* code to include

  • 0

In c\c++ can define:

#ifndef <token>
    /* code */
#else
    /* code to include if the token is defined */
#endif

my question, is there a way to do it in java? (which is not defining a global static variable..)
for example i want to run some code only in debug mode..

thanks!

  • 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-31T19:33:34+00:00Added an answer on May 31, 2026 at 7:33 pm

    The answer is No. Not in the sense that you mean.

    The way you do this kind of thing in Java as follows:

    private static final boolean flag = true;  /* or false :-) */
    
    if (flag) {
       /* code */
    } else {
       /* different code */
    }
    

    Java doesn’t have a preprocessor (like C and C++ do). However, the compiler will optimize away the unused branch of an if statement like the above, PROVIDED that flag is a compile-time constant expression. This is a limited form of conditional compilation. Note that the controlling flag constant can be imported from a different class.

    (IIRC, this behaviour is specified in the JLS … which means that you can rely on any conforming Java compiler to do it.)


    @Treebranch comments that “this” can cause code bloat.

    1. If @Treebranch is talking about object code bloat, this is not true. If you do this right with flags/expressions that are compile-time constant expressions as defined by the JLS, then the compiler does not emit any bytecodes for the “conditionally excluded” source code. See @edalorso’s answer.

    2. If @Treebranch is are talking about source-code bloat, I agree. But you can say the same thing for #ifdef conditional compilation. (Macros and #include can be used to reduce source-code bloat … but only at the cost of readability, maintainability, etc. And that was the reason that the Java designers refused to support any source-code preprocessing.)

    3. Java has a better way of dealing with platform differences, functionality variations and so on: use dynamic binding. If having lots of different plugin classes in your JAR is a concern (bytecode bloat), deal with it by creating a different JAR file for each platform, or whatever.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does each .h file starts with #ifndef #define #endif? We can certainly compile
There is one header a file Rectangle.hxx #ifndef Rectangle_included #define Rectangle_included #include <iostream> #include
#ifndef UNICODE #define UNICODE #endif #include <Windows.h> #include <cstdio> TCHAR* getSystemVolumine() { TCHAR volumine[2];
For example, how can I avoid writing the 'func_name' twice? #ifndef TEST_FUN # define
For example, I define a macro: #ifdef VERSION //.... do something #endif How can
I can define default value in domain by this way : class ProcessingPriority {
In Java you can define generic class that accepts only types that extend a
In HTML/CSS you can define a style which can be applied to many types
Why can't I get anything to display with this code? #include <iostream> #include GL/glfw.h
I have this include file ( memory .h ) #ifndef MEMORY_H #define MEMORY_H #ifdef

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.