Intro to FRC Programming - Romi
  • INTRODUCTION
    • Overview
    • Important Links
  • 💽Setup
    • Setting up the Romi's Software
    • Downloading Essential Tools
    • APCS vs FRC
  • How To Code in VSC
    • VSC- Intro
    • A Tour Through VSC
    • Creating a Regular Java Project
  • Intro to Java
    • What is Java?
    • Beginning Steps
    • 🟩Fundimentals of Java
      • Variables
      • Operations
      • Methods
      • Comments
      • If Statements and Conditions
      • Boolean Zen
      • Loops
      • Challenge- Create a Box House
    • 🟨Advanced Concepts
      • Objects
      • Scanners
      • Null Objects
      • Arrays
      • Errors
      • For-Each Loops
    • 🟥Object Oriented Programming
      • Basics of OOP
      • Instance Classes
      • Static Classes
  • 🕑Romi Curriculum- Timed Base
    • Romi Curriculum- Introduction
    • Creating a WPILIB Project
    • Running Your Code
    • The Robot Class
    • Subsystems
      • RomiDrivetrain
    • Cool stuff i will rename this category later
      • Spark Motors
      • PIDs
      • External Controllers
      • Encoders
  • 🖥️Romi Curriculum- Command Based
    • Command Based Code
    • RobotContainer
    • Commands
    • CommandScheduler
  • UNRELATED IMPORTANT CODING KNOWLEDGE
    • Constants
  • SAMPLE CODE
    • Tank Drive Example
      • RobotContainer
      • TankDriveSubsystem
      • MoveRobotCommand
    • Worktop Systems Sample Java Code
      • Belt Elevator Sample Code
      • Rotating Arm Sample Code
Powered by GitBook
On this page
  • What are they
  • Creating class objects (instance classes)
  • Constructors (Only for instance classes)
  1. Intro to Java
  2. Object Oriented Programming

Instance Classes

What are they

Instance classes are classes that are called as objects. These classes have certain things unique to them such as constructors.


Creating class objects (instance classes)

Objects are similar to variables in that they hold values and data. Similar to the String data type, they are also reference types and can hold methods. You can create these classes similar to creating a variable as well. Here is the Java syntax:

<className> <object name> = new <className>();

Constructors (Only for instance classes)

Class constructors are methods that run once automatically when an object of that class object is created. Class constructors allow you to add parameters, allowing for more versatility with class objects.

Creating class constructors

Declaring a constructor is similar to declaring a class method with parameters, the only difference is that you don't declare a return type and the constructor must have the same name as the class, this is because they never return anything. Constructors are usually used to assign variables to certain values. Here is an example of creating a constructor in a new example class that assigns exampleInt to a given parameter:

public class Example{
    int exampleInt; // declaring a variable without a value
    public example(int exampleInt){
        this.exampleInt = exampleInt;
    }
    public void addToInt(int add){
        exampleInt += add;
    }
}

One thing in the code above that you may not know is the this identifier. The this identifier allows you to refer to variables of the same name outside of the scope of a method.

Scope refers to the region of code where a piece of data (like a variable) can be used since variables or parameters instantiated inside a method cannot be directly accessed from outside the method's { } brackets:

Using constructors

Using class constructor parameters is basically the same as using method parameters. This is because all you have to do is put your values inside the parentheses (). Here is an example of the example class object ex:

/* Create a new object and use the 
constructor to assign a value to exampleInt */
Example ex = new Example(17); 
// Now exampleInt in the ex object is 17

PreviousBasics of OOPNextStatic Classes

Last updated 7 months ago

🟥
Drawing