Constants in Interface [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I know there were a lot of questions about putting constants into interfaces, and that is a bad practice.
But I have a question, what if I have a class, where I want to define some sets of constants, but I want to have them in groups, is it a bad practice to have interface/class inside a class and define constants in there, so my constants are grouped?
e.g.
public class MyClass{
public final class A {
public static final String A = "a" ;
}
public final class B {
public static final String B = "b";
}
....
}

Yes you can have nested class to group the constants. It's really not a bad practice.

the question leads to a misleading. From my point of view the necessity to group constants is related to the fact that they regards different classes and so they must be putted int the correct classes; in other words this question can be an alarm about a not perfectly correct class design.

Related

When to use public modifier for a class field in OOP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
When programming in OO languages like C# or Java, is there a good situation where declaring a public field inside a class is actually valid (I myself always use a property for not making the user of the class depend on the data and to support data protection)?
Otherwise, it feels weird that C# for example allows you to do so.
According to the C# coding conventions public field should be used sparingly:
// A public field, these should be used sparingly
public bool IsValid;
Why? I think because of:
can be edited by any other user of class
if you want to add some logic to field, then you need to create property instead of field. By doing this, you will break a contract of class
it is not possible to override variable
However, there is a case when you need to have field as #VGR said:
public const string foo = "";

Declaring a constant in java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have come across something rather strange today. A java developer has declared a constant like this:
public final class Constants {
public static final String KEY_COLUMN = AbstractDataRow.standardiseColumnName("key");
... // rest of implementation
}
public abstract class AbstractDataRow implements IRow {
public static String standardiseColumnName(String columnName) {
return columnName.trim().toLowerCase();
... // rest of implementation
}
}
What is strange to me is that KEY_COLUMN calls a method from an abstract class.
Is this good practice?
answering your question.. Not really.
You could just as easy call that string into the method already defined.. then convert that string to lowercase.
send the string to the method instead of the method to the string.

What merits do we have making a private inner class final? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I went through some code today. And I spotted something like this:
public class SomeClass
{
...
final private class SomeHandler implements Blahblah
{
...
}
}
Since no one should be able to access SomeHandler from outside, I fail to see why we should make it final. Does anybody have some different insight?
Declaring a class as final has two related but distinct purposes.
It tells the compiler "do not allow this class to be extended". This is done primarily to prevent mistakes.
It tells the reader "do not worry that this class might be extended". This is to aid understanding.
In the case of a private final inner class, we can assume that one person writes or modifies the class and that they do it after understanding the design. Thus, the likelihood of someone mistakenly extending a class that shouldn't be extended (by design) is small.
However, someone reading the code who needs to know is the private inner class might have been extended needs to scan the entire Java source file ... unless the class has been declared final.
Hence final could be serving a useful purpose ... in making the design manifest to improve readability.
On the other hand, the final in the example you found could have been added without any particular intent; i.e. it could simply be "redundant".

package structure for a class that contains only constants [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a realy simple class that contains only constants:
public class AppData {
public static long SPLASH_SCREEN_DELAY = 3000L;
}
my question is in which package should I place the class and what should be the name of this package?
For example...
Activities are placed in:
de.appname.ui.activities
domain classes in:
de.appname.model
a class that contains only static informations:
(???)
I know it's a design question and I need you suggestion.
Best regards
Stefan
You can keep this class under de.appname.util or de.appname.utility because all the constants will be utilized by other classes.

Making parameter of main method as final [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i saw a different signature of main() i.e.
public class Demo {
public static final void main(final String[] args) {
.............
.............
}
}
What is the benefit to make main()'s argument as final. please explain!
Method parameters in Java are local variables. Declaring them as final allows you to access them from inner anonymous classes. Other than that, it can help prevent potential bugs that happen when you change parameter values.
So in your example code, using final String[] args will work just fine.
String[] args is just a variable declaration, it corresponds to the command line parameters, so it can be final.

Categories

Resources