Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   WPI cookbpook help (http://www.chiefdelphi.com/forums/showthread.php?t=112815)

DPG2013 05-02-2013 16:33

WPI cookbpook help
 
I am kind of new to Java programming and I was looking through the WPI cookbookhere and under the command robot tutorial I saw this:

public class Chassis extends Subsystem {
public static final int LEFT_MOTOR_PORT = 2;
public static final int RIGHT_MOTOR_PORT = 1;
public static final double TURN_AMOUNT = 1.0;
public static final double FORWARD_SPEED = 1.0;
private static Chassis instance = null;
private RobotDrive drive;
public static Chassis getInstance() {
if(instance == null) {
instance = new Chassis();
instance.setDefaultCommand(new DriveWithJoystick());
}
return instance;
}
private Chassis() {
drive = new RobotDrive(LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT);
drive.setSafetyEnabled(false); // to simplify explanation (not good practice)
}
public void straight() {
drive.arcadeDrive(-FORWARD_SPEED, 0.0); // start driving forward
}

public void turnLeft() {
drive.arcadeDrive(0.0, TURN_AMOUNT); // start turning
}
public void driveWithJoystick() {
drive.arcadeDrive(OI.getInstance().getJoystick());
}


I pretty much understand every thing except the instance part. What is a instance and how is it being used here.

gixxy 05-02-2013 18:35

Re: WPI cookbpook help
 
A class is the blueprint for an object.

An instance is an object of a class.

Basically what that code is saying is: "Only one instance of Chassis will exist, and it is this one. In order to use it you need to get it from me."

another example: the drive object in that code is an instance of RobotDrive.

Code:

private RobotDrive drive;

DPG2013 05-02-2013 19:20

Re: WPI cookbpook help
 
That makes sense thank you.


All times are GMT -5. The time now is 09:51.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi