2

I want to declare a hex decimal value as a constant character with in a static constants class as shown below:

public static class Constants
{
    public char[] Record_Separator = new Char[] { '\x01E' };  //Record Separator
}

I know within a static class it's not possible to instantiate char as I wanted to retain all constants with in a single class so want to know if there is another way to do the same.

3
  • I think this is C#, though I don't know if Java is capable of this as well. Commented Mar 14, 2013 at 12:07
  • Can some one moderator remove java and put C# in Tag it was by mistake I selected java. Commented Mar 14, 2013 at 12:24
  • Upper_Camel_Case ? I'm not sure I've encountered this before and I'm not sure I like it... Commented May 30, 2013 at 3:52

4 Answers 4

2
public static class Constants
{
    public static readonly char[] Record_Separator = new Char[] { '\x01E' };  //Record Separator
}
Sign up to request clarification or add additional context in comments.

Comments

1
public class Constants {
    public static final char[] RECORD_SEPARATOR = { 0x1E };
}

Notes: Your constant should be static and final, otherwise it's not a constant. You've used Char instead of char. The common naming convention for static final constants in Java is ALL_UPPERCASE.

You cannot make a top-level class static.

3 Comments

it cannot be defined static, but it can be final with a private constructor
The question is about C#, not Java. ;-) I won't downvote, though :-D
Pfff... ;-) @ebeeb by the way, I didn't downvote your answer.
0
private const double MyConst = 0x01E;

What about this?

3 Comments

Why double?? The question is about Java, not C#.
You obviously popped in, when the the question was already retagged with java. The only tag was static
Well I have come up with a workaround for this issue i.e. decalring the all the varible static rathere class. But this doesn't seem to be good practise can somebody comment on this and suggest the best approach. public class Constants { public static string n1="abc"; public static string nn="abn"; public static char[] Segment_Separator = new Char[] { '\x01E' }; } public static class Constants { public const string n1="abc"; public const string nn="abn"; }
0

How about just using the static constructor?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.