Which modifier should go first? [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java modifiers syntax and format
You can put modifiers of variables or methods in any order in Java, for example:
private static final int x;
static final private int y;
Both lines of code above work fine, and both declare an integer that is private, static, and final. My question is, what is the accepted/common standard for arranging them? What order should I put the modifiers in?

See http://checkstyle.sourceforge.net/config_modifier.html (which gives links to the language specification)
The preferred order for your example is:
private static final int x;

private static final int y is probably better. It is the decreasing order of interest from outside of the class.
When you look at 'public-private' tell whether this method/variable is of any interest to any external client.
next is static. This is mostly a class context information.
Having final as last option- This information mostly needed only by class's method.
(I generally leave it my editor to sort the modifiers- it conforms to above order)

I have made the same question but with .NET, and I received a really good answer that I want to share with you: Order of function modifiers in C#
Hope that helps,

I would say is the common way would be
private static int x;
Though I am not sure were you would put the final as the common way.
If I was doing it I would probably do it the way you had it though.
private static final int x;

Related

Shoud I use final keywords of method params in Java? [duplicate]

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.

Static and Final Rules of Interface [duplicate]

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

Is static a useful modifier for a private string [duplicate]

This question already has answers here:
Should a private final field be static too?
(5 answers)
Closed 3 years ago.
In my team we had a small discussion if a field declared 'private static final' in a class has any advantages from just declaring it 'private final'.
For example if I have the following line in my class:
private static final String a = "a String";
Is this really better than just declaring it like this:
private final String a = "a String";
If it would be a variable that can be used outside of this class, declaring it static makes total sense. But as it is private I see no real advantage apart from a static variable being only created once and is than referenced in all objects. We have lots of tests and those define variables like this. Those tests are only run once when the system starts and are no longer needed after that. Apart from that is there anything I am missing?
In short:
You use private final when you only need to set the value once, but the value needs to be different for each instance of that class.
You use private static final when all instances of your class need to have access to one unchanging value. It is useful for avoiding magic strings / numbers and enhancing readability, particularly if your value is a longer string. If you start defining too many of these, consider moving them to a configuration file.
So static in combination with final and private is certainly useful, but staticin general requires careful, deliberate usage. Don't just slap it on everything; instead, your default action should be to analyze whether or not you need it, and implement accordingly.

A comparison of constants and variables in Java and Swift

In Android/Java when I make constants and variables I generally do something like the following:
public/private static final int MY_CONSTANT = 73;
int myVariable = 37;
And in Swift I do
let myConstant: Int = 73
var myVariable: Int = 37
or just
let myConstant = 73
var myVariable = 37
Are the Swift versions equivalent to the Java versions under the hood?
I don't have a specific coding problem right now, but I am trying to gain a deeper understanding of the inner workings of both languages. The question came to mind when I was reading about Swift properties. Sometimes making a comparison or having a frame of reference helps me to do that.
I'm not sure if this question is appropriate for SO or not. I'll try asking and if it gets too many downvotes I'll delete it.
Using let in Swift is like specifying final in Java, while using var in Swift is like leaving out final in Java.
Swift also have Type Properties that are global to the type. The keyword for type properties is static, so it's exactly the same as in java. If you want a, in java terms, static final field then you write static let, and if you just want a static non-final field you write static var.

which order is better for field declaring

private static final Logger LOGGER = Logger.getLogger(AbstractDbClient.class);
protected Connection connection;
protected Connection connection;
private static final Logger LOGGER = Logger.getLogger(AbstractDbClient.class);
which order is better for field declareing? some books pointed that should order them by private/public/protected/etc, if base on this , the second code is better, but it seems looked bad. If incluse static final var or static var? which order rule is?
I suggest you keep it consistent, but I prefer to arrange fields in the order they are set as this makes it easier to understand the code and debug it. IMHO.
static final fields
final fields
mutable fields.
A common standard is to put all public variables on top, followed by protected, and then private. Some people put class/static variables on top before instance variables, and some put them after.
If you are working in a team that's writing new code, it would be best to get together and decide on a common convention. If you already have existing code that you are adding to, then go through it and figure out what convention the previous authors followed. You don't want a mix of styles in the same codebase.
Data layout actually has performance properties, besides ordering the fields as you feel comfortable with.
I tend to follow something like:
static final fields,
static fields
final fields
modifiable fields
volatile fields grouped by use cases
and sometimes
private field usedOnlyInFoo
method foo(){
}
More on the topic why data layout matters. While there is no formal way to enforce data layout in Java besides arrays/Direct Buffers usually the compiler (JVM) places the fields in their declaration order.
Personally I like all my statics at the top of a class and have all fields in public, protected, default, private order.
eg
public static final Integer a;
private static final Integer b;
public Integer c;
Integer d
private Integer d;
I also tend to add a line between teh statics and non static fields.
But it is a matter of opinion. Perhaps ask your peers whom you work with. It's better to be consistent on this sort of thing.

Categories

Resources