0

Google isn't being very friendly today, and I'm researching some OOP techniques I've not used in the past.

Basically, I've noticed some libraries have variables with preset choices, like new Website(Websites.STACKOVERFLOW). What is that called, when you've got apparently immutable values like that? What if I want to create my own one, with Colours.RED and Colours.GREEN?

I hope you can tell me what this is called to allow me to continue my research! Thank you.

EDIT: I'm not marking this as a duplicate because I couldn't figure out how to accurately describe what I was looking for - I'm thinking that it's quite possible someone else may have similar difficulties and might find this useful. If I'm wrong, that's okay.

Forest
  • 938
  • 1
  • 11
  • 30

4 Answers4

4

They are called enumerations. You can find detailed info here

They are defined as:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

and used as:

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Mondays are bad.");
                break;

            case FRIDAY:
                System.out.println("Fridays are better.");
                break;

            case SATURDAY: case SUNDAY:
                System.out.println("Weekends are best.");
                break;

            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}

and the output would be:

Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.
Chanandler Bong
  • 403
  • 1
  • 11
  • 23
Alp
  • 3,027
  • 1
  • 13
  • 28
3

Not seeing the exact code you reference I assume from the syntax style that Websites.STACKOVERFLOW is a constant; and that Website(Websites.STACKOVERFLOW) is creating a well known web site called Stackoverflow.

The class Website and Websites might look like:

public class Website {
  public WebSite(String wellKnownWebSite) {
    ..
  }
}


public class Websites {
  public static final String STACKOVERFLOW = "http://www.stackoverflow.com";
}

It is also possible that this syntax is using Java Enumerations, technically Enums (It is just a contraction).

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
1

It's called an Enum. Check here for more information.

Luke Bajada
  • 1,737
  • 14
  • 17
0

It may be two things, Either an Enumerator (AKA Enum) or a class with constant fields in it. Enum Example:

public enum ValueSeperationType {
    CSV(","),NEW_LINE("\n"),DASH("-"),DOT("."),SPACE(" "),TAB("\t"),OTHER("_"); //Our values, the parenthesis with the string in it is the value the constructor will get once the enum is called.

    private final String seperator; //A variable to hold the value provided to the constructor by the enum;
    ValueSeperationType(String seperator){// the constructor that gets called when we type for instance "CalueSeperationType.CSV" Then the constructor takes as parameter a String with value ",".
        this.seperator = seperator;// initialising the variable to hold the value.
    }

    public String getSeperator(){ // a method allowing user to get the seperation value, If we type ValueSeperationType.DASH.getSeperator() this will return "-".
        return this.seperator;
    }
    //You can add as many parameters in the constrctor as you want, as many variables and methods as you want.
}

The other possibility is that it is a class containing Constant fields. (Although it is unlikely because that is not the proper naming convention but... you never know.) So an example of a class with constant fields:

class ValueSeperation{
    public static final String CSV = ",";
    public static final String NEW_LINE = "\n";
    public static final String DASH = "-";
    public static final String DOT = ".";
    public static final String SPACE = " ";
    public static final String TAB = "\t";
    public static final String OTHER = "_";

    //Same concept as above, in a more direct way... here you simply invoke ValueSeperation.CSV and that returns ",". Each method has it's benefits and it is up to the developer to decide what to implement depending on the senario.
}

I hope that helped.

fill͡pant͡
  • 1,147
  • 2
  • 12
  • 24