Hi, I am new to FRC programming, I usually use Python when I program. My team (3767) uses Java. Is there anyway to call the variable (Joystick stickOne = new Joystick(0); in a different class than its located in? All the things I’ve tried haven’t worked. I have tried importing the Port class into the drive class and calling it using Port.stick, but that doesn’t work. Any help would be greatly appreciated!
You have to make sure that you are passing an instance of the port class because stickOne is not a static field, but an instance field. if instead you write
public static Joystick stickOne = new Joystick(0);
then it should work. If you dont want the field to be static, make sure that you pass an instance of the Port class and make sure that stick one is public:
public Joystick stickOne = new Joystick(0);
Id do the second option as there will be less issues
Making the joystick a global (i.e. public static
) will work, but I strongly recommend against doing that because it creates hard dependencies on your input choices throughout your codebase.
A better pattern is to pass the joystick object to where it’s needed as a constructor or method parameter - or, better, to accept a functional interface (e.g. DoubleSupplier
) in its place and pass a lambda wrapping the relevant joystick function. The current command-based example projects give a good example of how to do this in practice (it is not a pattern specific to the command-based framework).
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.