#Accessing private enum in another Class

44 messages · Page 1 of 1 (latest)

small helm
#

Hi so basically i have created Class Vlak
Which looks like this

class Vlak
{
    private String id;
    private  enum types { rychlik,osobni,spesny}

    public Vlak(String id)
    {
        this.id = id;
        
    }
    
    public void vypis()
    {
        System.out.println(id);
    }

}

But now im trying to understand how can i access it through function and then assign some of the values to the object.

blissful talonBOT
#

Hey, @small helm!
Please remember to /close this post once your question has been answered!

blissful talonBOT
#

<@&765578700724371486>

Requested by chewskot#0553
keen wind
#

By definition, if it is private then other classes don't have access to it

small helm
keen wind
#

What do you mean?

small helm
#

right ?

keen wind
#

You can, but other classes can't know the types that these getters and setters use

#

The setters would be nearly useless, and the getter would just return Enum as far as the other classes are concerned

small helm
#

oh okay, so if they were public how could i work with them ?

keen wind
#

If they were public then it's not special

#
Vlak vlak = new Vlak();
vlak.setType(Vlak.types.rychlik);
Vlak.types type = vlak.getType();
keen wind
#

...... Yes you still need to write the getters and setters

small helm
keen wind
#

? As usual

small helm
#

i guess im dumb but ive never used enums soo this is giving me errors

stable tendon
#

But you want to assign Vlak.types to Vlak.types

#

Create field with corresponding type and make getter and setter work with it

#
In Vlak:
...
private types type;
...
public void setType(types type) {
    this.type = type;
}
#

Btw rename types to Types. Enums are also written in CamelCase

small helm
stable tendon
#

yes

small helm
#

but the .getType gives me this error

stable tendon
#

you've renamed the class

#

so maybe you're compiling other code/unsaved code

#

because now you don't have anything named types, only Types

stable tendon
#

public types getType

#

replace with

#

public Types getType

small helm
#

oh

#

i see

#

thx

#

❤️

stable tendon
#

ur welcome