Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

STRING LITERALS (private static final vs public static final)

807605Jun 12 2007 — edited Jun 12 2007
Hey people,

I am just wondering if someone please help me confirming which one is the best practice in java to use String literals as constant like:

class A {
private static final String ABC = "ABC";
}

or

class B {
public static final String ABC = "ABC";
}

As far as I know String literals (with same contents) references refers the same object.

So If I declare private static final String ABC = "ABC". In what scenarios does this make sense? only if I declare a different string literals with "ABC" in the same class ???

For a scenario in a project of close to 3000 java classes, where I dont expect the literals to come in same class but same String literals can be defined in different classes.

Is it wrong or bad practice to have the same String literal in different class with public static final String ABC = "ABC" (even if its already defined in other classes)?

Thanks,
-Amit
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 10 2007
Added on Jun 12 2007
9 comments
2,010 views