Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of #define in Java?

I'm writing a library that needs to have some code if a particular library is included. Since this code is scattered all around the project, it would be nice if users didn't have to comment/uncomment everything themselves.

In C, this would be easy enough with a #define in a header, and then code blocks surrounded with #ifdefs. Of course, Java doesn't have the C preprocessor...

To clarify - several external libraries will be distributed with mine. I do not want to have to include them all to minimize my executable size. If a developer does include a library, I need to be able to use it, and if not, then it can just be ignored.

What is the best way to do this in Java?

like image 715
Justin Avatar asked Dec 21 '09 22:12

Justin


People also ask

Is it equivalent to or equivalent of?

When equivalent is used as a noun, the most common preposition is of: 150 grams are the equivalent of a medium-sized potato. James thought retiring is the equivalent of a death sentence. Seven human years are the equivalent of one dog year.

What does it mean for equivalent?

Definition of equivalent 1 : equal in force, amount, or value also : equal in area or volume but not superposable a square equivalent to a triangle. 2a : like in signification or import. b : having logical equivalence equivalent statements. 3 : corresponding or virtually identical especially in effect or function.

What is equivalent and example?

In math, equivalent is different from equal. Equal means same in all aspects, whereas equivalent means similar but not identical. For example, 2 is said to be equal to 2 but equivalent to 1 + 1.

Is equivalent a noun or verb?

EQUIVALENT (noun) definition and synonyms | Macmillan Dictionary.


1 Answers

There's no way to do what you want from within Java. You could preprocess the Java source files, but that's outside the scope of Java.

Can you not abstract the differences and then vary the implementation?

Based on your clarification, it sounds like you might be able to create a factory method that will return either an object from one of the external libraries or a "stub" class whose functions will do what you would have done in the "not-available" conditional code.

like image 65
Steve Emmerson Avatar answered Sep 19 '22 13:09

Steve Emmerson