This question already has answers here:
Why should I use the keyword "final" on a method parameter in Java?
(12 answers)
Closed 1 year ago.
I have a look at several questions e.g. In Java, should I use “final” for parameters and locals even when I don't have to? on SO and I am a little bit confused after reading the suggestions and answers.
In the project that I ma working on, there are lots of final keywords for the method parameters in methods and interfaced as shown below:
interface:
MenuDTO findMenu(final UUID menuUuid);
implementation:
#Override
public MenuDTO findMenu(final UUID menuUuid) {
}
As far as I know, using final keyword for method parameters as shown above is pointless. So, should I remove the final keywords from the interface methods and their implementations?
I also see no meaning in using final in parameters in Java. The final means you cant cant reassign value to variable. This you cant reassign anyway since the value is parameter. It looks for me like this style of writing final is coming from someone who worked with C++ before Java.
Related
This question already has answers here:
Which types can be used for Java annotation members?
(4 answers)
Closed 4 years ago.
Can I able to call a method which returns string inside an annotation.
If so please guide me how to achieve this?
I tried like this but this doesn't work for me.
#Description(value = Resource.getWord("key"))
An annotation only takes compile time constants (as they might be used during compile time), therefore you cannot make any calculation within the definition, as they are unknown during the compile time.
Allowed constant types are (taken from java-annotation-members):
Primitive
String
Class
Enum
Another Annotation
An array of any of the above
Possible solution for your situation:
As I understand you would like to localize the #Description content.
As this is only meant to be exposed to other developers anyway, you are safe to simply use English, in my opinion. Localization is for the end user, not the developer.
I can imagine an aspect being wired up to process methods annotated like this, where the "key" is in the annotation, and the aspect processing then uses the key at run time... but I'm not sure this is what you're looking for.
This question already has answers here:
Is "public static final" redundant for a constant in a Java interface?
(6 answers)
Closed 3 years ago.
What exactly the meaning of instance fields in JAVA ?
As per am knowing in JAVA :
An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
But, When I have tried as below :
interface TempIn
{
TakeInput tv=null;
String name="";
int temp=0;
void printT();
}
and it's working. How ?
Confused...
Simple: all these fields are static and final by default.
Therefore the java language allows you to write down something that is implicitly given.
In other words: imagine the "compiler" putting down the keywords for you.
But I agree, this is a bit of confusing. And it also turns a bit into a "style" thing. In the early years of Java, a lot of people would add these redundant keywords to their interfaces. On the other hand, "clean code" tells us to avoid redundancy in our code. And nowadays, an IDE like IntelliJ will even give you warnings when using the keywords. So, my recommendation:
don't touch old, existing code
talk to your team, and decide what makes sense for you, and for new code, follow that agreement
This question already has answers here:
What is the difference between a.getClass() and A.class in Java?
(7 answers)
Closed 6 years ago.
I noticed when looking through some of the Java code at my work that when we initialise the logging framework in each class we have something like:
private final Logger log = Logger.getLogger(Foo.class);
This got me thinking, is there any reason that we use Foo.class rather than:
private final Logger log = Logger.getLogger(this.getClass());
Is this simply a preference or is there a practical reason to prefer one over the other?
Edit: originally the code snippets referenced static members, which obviously wouldn't have compiled.
Ignoring the bit about static contexts (which is totally correct - let's assume your expressions in those initializers get moved to instance methods instead) - a method with this.getClass() will have very different results when called from a subclass!
If you want the class object for Foo, always, use the first version.
If you want the class of the caller (a subclass of Foo), use the second version.
This question already has answers here:
Why doesn't Java allow enum to be defined within a method? [duplicate]
(3 answers)
Closed 8 years ago.
Java does not allow enum to be declared in a method that is a basic java syntax.
But
Could any body explain why is that, what could have gone wrong if it would have been allowed by Java, I am sure there must be some cause behind this restriction, any idea?
class Example {
void aMethod() {
//This is not allowed
enum Status {
NEW,
PROCESSING,
COMPLETED;
}
}
}
enum types are typically used to share constant values between classes so declaring them in the scope of a method wouldnt make any sense
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Protected methods in objective-c
I am a Java developer who is starting to have questions about Objective-C coding syntax.
As of right now, I only see +/- that denotes the Java public/private equivalents.
How would you implement a protected method in Objective-C?
+/- indicates whether a method is a class method or an instance method -- not whether it is public or private.
In short, you can't declare a method as protected in Objective-C. Essentially every method is public, although if you don't declare a method in the header file, it is, in effect, private.