0

I have a class BaseNumber with a protected static enum Base, and another class ArithmeticBaseNumber extends BaseNumber.
I can access the Base enum from BaseNumber, however not from ArithmeticBaseNumber.
Is there a way to access the enum?
Thank you!

Edit - The code:

public class BaseNumber {
    protected static enum Base {
        X, Y, Z
    }
}
public class ArithmeticBaseNumber extends BaseNumber {}

The problem is that BaseNumbe.Base.X works fine, however ArithmeticBaseNumber.Base.X gives "The type ArithmeticBaseNumber.Base is not visible" error.

5
  • 1
    Why don't you show us the code? It'll make it easier to see the problem here. Commented Oct 21, 2020 at 7:09
  • @MCEmperor I added the code. Commented Oct 21, 2020 at 7:20
  • 1
    It follows the rules of protected members. But why do you want to access the constant via ArithmeticBaseNumber.Base.X? It’s still the same constant as when being accessed via BaseNumber.Base.X. There is no advantage in using the other expression. Commented Oct 21, 2020 at 18:05
  • @Holger I want to deny the programmer from using BaseNumber class at all. Commented Oct 25, 2020 at 2:56
  • 1
    You can’t deny using a public class. Besides that, using ArithmeticBaseNumber.Base.X still implies using BaseNumber, as Base is a nested type of BaseNumber. Commented Oct 26, 2020 at 9:31

3 Answers 3

1

Java doesn't support the inheritance of static members, a static member is callable on a class in which it has been created.

Sign up to request clarification or add additional context in comments.

Comments

0

It can be accessed. Try below example.

package com.example.demo.enumexample;

public class BaseNumber {
protected static enum Base{
FIRST("first"),
SECOND("second"),
THIRD("third");
String httpMethodType;
Base(String s) {
    httpMethodType = s;
}
public String getHTTPMethodType() {
    return httpMethodType;
}
}

public static void main(String[] args) {
    Base baseNum = Base.FIRST;
    System.out.println(baseNum);
}
}

And in ArithmeticBaseNumber.java

package com.example.demo.enumexample;

import com.example.demo.enumexample.BaseNumber.Base;

public class ArithmeticBaseNumber extends BaseNumber{
public static void main(String[] args) {
    Base baseNum = Base.FIRST;
    System.out.println(baseNum);
}
}

Output will be :

FIRST

In your example code,

package com.example.demo.enumexample;

public class BaseNumber {
protected static enum Base{
    X, Y, Z;

}

public static void main(String[] args) {
    Base baseNum = Base.X;
    System.out.println(baseNum);
}
}

And

package com.example.demo.enumexample;


public class ArithmeticBaseNumber extends BaseNumber{
public static void main(String[] args) {
    Base baseNum = Base.X;
    System.out.println(baseNum);
}
}

Output will be :

X

Comments

0

The one enum type you're defining is BaseNumber.Base and not ArithmeticBaseNumber.Base nor simply Base.

So just reference it by its proper name BaseNumber.Base, and everything is fine.

You can (but probably shouldn't) make the simple name Base work by importing:

    import mypackage.BaseNumber.Base;

Caveat: if you want to use the short name, I'd typically recommend moving the Base enum out into its own file - to me, the dotted syntax is an important reminder that we're talking about some internal element of BaseNumber.

As a similar case, for readability reasons, I always stay with Map.Entry instead of Entry (unclear that it's related to Maps) or HashMap.Entry (plain wrong, Entry isn't part of HashMap).

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.