0

I want to use inst in another class SubscribeTables() I need it with all of its data. I tried to make a getter in Main() but it didnt work. Maybe i can pass it to another class somehow? Can someone help me with it?

Lets say i need to call inst.isConnected() in SubscribeTables()

public class Main {
    public static void main(String[] args) throws IOException {

      // Setup NT4 Client
      NetworkTableInstance inst = NetworkTableInstance.getDefault();
      inst.startClient4("FRC Stat Track");
      selectNetworkTablesIP(inst, 5883);
      // Connects after ~100ms


      new SubscribeTables();
}

2
  • You can create a constructor for SubscribeTables which takes a NetworkTableInstance as a parameter, and create a field in SubscribeTables. Commented Jan 25, 2023 at 22:25
  • Why is all of your code in the static main world? If you need OOP objects that interact, then use OOPs. Declare your inst field as an instance field with getters/setters and usual restrictions and controls. Commented Jan 25, 2023 at 22:26

3 Answers 3

1

So to consolidate, in terms of code, your SubscribeTables class should look like this:

public class SubscribeTables {
    private NetworkTableInstance instance;

    // Make a constructor to take NetworkTableInstance
    public SubscribeTables(NetworkTableInstance instance) {
        this.instance = instance;
    }

    public void function() {
        // Use the NetworkTableInstance for every function in this Class
        boolean isConnected = instance.isConnected();
    }
}

And the way to create a SubscribeTables object:

SubscribeTables tables = new SubscribeTables(inst);
Sign up to request clarification or add additional context in comments.

Comments

0

The scope of NetworkTableInstance inst is limited to the lifecycle of main method as your new class is not aware. You have 2 options if you still want to do it in this way - but not recommened.

  1. Pass the instance of NetworkTableInstance as a constructor paramater to SubscribeTables

  2. Pass the instance of NetworkTableInstance to the respective methods as a method parameter you need to call on SubscribeTables

1 Comment

I couldn't do something like NetworkTablesInstance inst = new NetworkTablesInstance();
0

You can supply link of object A(type NetworkTableInstance) to object B(type SubscribeTables) either by argument in constructor or by argument in setter method. In both cases SubscribeTables class should have field of type NetworkTableInstance to put NetworkTableInstance object from constructor or setter arguments into the field.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.