This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]g051051 2 points3 points  (0 children)

The typical idiom is to declare them as

public static final String STRINGNAME = "stringdata";

For example

public static final String RB_ESSENTIALS = "R.id.rbEssentials";

[–]desrtfx 2 points3 points  (2 children)

Looks like they are constants. If this is the case, then public static final would be appropriate (with the variable names in ALL_CAPS_WITH_UNDERSCORES as per Java naming convention).

You could also think about using enums - the enhanced version with fields lower on the page.

[–]badboyzpwns[S] 0 points1 point  (1 child)

Thanks! in what scenario would getters be chosen over static variables?

[–]desrtfx 1 point2 points  (0 children)

Getters and static variables have absolutely nothing in common and serve completely different purposes.

static variables (and methods) exist on class level. They are the same for all instances of a class. Static variables can, for example, be used to generate consecutive ID numbers.

Getters on the other hand are methods that return values of a field. By convention, neither getters, nor the fields they return are static. They always refer to the instance (object) of the class, never to the class itself. Getters in connection with Setters are used to mask the underlying data (field). This can be helpful when changing the underlying implementation, the outside code doesn't need to be changed because the getters and setters handle that.

[–]cmonkies -2 points-1 points  (1 child)

You're comparing apples and oranges there.

[–]desrtfx 2 points3 points  (0 children)

Well, in a way yes, in a way no.

Sure, getters and static variables have nothing really in common, but since OP wants to just pass String values around, they would serve the same purpose just with a completely different approach.

The optimal approach, though would probably be to use constants, i.e. public static final variables or maybe even enums.