How to import all Static variables from another class?

Asked

Viewed 405 times

0

I have two classes, A and B. The class A need to access all existing static variables in B. How to make this import?

I found a lot to do with get, however it does not seem to be that. I am beginner and I am lost.

  • would you be able to put the code you are trying to do? So it helps more people to help you.

  • I think you could post your codes

3 answers

5

Put that in class A:

import static B.*;

2


regardless of whether you instanciar class B or use track import... you will only be able to access your attributes if they are declared as public Static.

If you prompted class B, you probably came across the following error!

Change visibility of 'variable' to public
Creat getters and setters for 'variable'

Access via Import, you just need to call it that: Ex: B.Nome or Class.Atributo

1

To access all static variables existing in B you must write the following import:

import static B.*;

Thus, class A will have access to all members declared as static that A can see. Members means "variables" and "methods".

If I wanted just one variable, I could do something like this:

import static B.VARIAVEL_ESTATICA;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.