A simple kind of method that returns a value is what is formally called an accessor because it accesses a value in an object. In the real world everyone calls them getters.
A getter is a method whose job is to give you information about an object.
A simple kind of method that returns a value is what is formally called an accessor because it accesses a value in an object. In the real world everyone calls them getters.
A getter is a method whose job is to give you information about an object.
2.2. Creating and Initializing Objects: Constructors¶
Class → blueprint Object/Instance → thing created from the class Variable → named container Reference → the value in the variable that points to the object
A Java class defines what objects of the class know (attributes) and what they can do (behaviors). Each class has constructors like World() and Turtle(habitat) which are used to initialize the attributes in a newly created object. A new object is created with the new keyword followed by the class name (new Class()). When this code executes, it creates a new object of the specified class and calls a constructor, which has the same name as the class. For example, new World() creates and initializes a new object of the World class, and new Turtle(habitat) creates and initializes a new Turtle object in the World habitat. // To create a new object and call a constructor write: // ClassName variableName = new ClassName(parameters); World habitat = new World(); // create a new World object Turtle t = new Turtle(habitat); // create a new Turtle object
The thing inside the parentheses must match the constructor's parameter type.
Overloading is when there is more than one constructor. They must differ in the number, type, or order of parameters.
lets you make multiple objects with different parameters
A constructor signature is the constructor name followed by the parameter list which is a list of the types of the parameters and the variable names used to refer to them in the constructor.
the one that actually prints out your stuff
Constructors initialize the attributes in newly created objects. They have the same name as the class.
A special method that runs when an object is created to initialize (to give an object or variable its STARTING value) an object
You can also think of a class defining a new type. Just like you use int to declare variables that can hold an whole number value, you can use Turtle to declare a variable whose value has to be an instance of the Turtle class. And just like the Java compiler will only let you do things with the values of int variables that make sense (like adding and multiplying them), it will only let you do things with values of a Turtle variable that make sense to do with turtles, namely accessing the instance variables and methods defined in the Turtle class. The following picture has lots of cats (objects of the type cat). They are all different, but they share the same attributes and behaviors that make up a cat. They are all instances of cat with different values for their attributes. Name some of the attributes and behaviors of the cats below. For example, the color (attribute) of the first cat is black (attribute value) and it is playing (behavior).
attributes: NOUNS/ADJECTIVES.
behaviors: VERBS/ACTIONS.
In Java code, the attributes are written as instance variables in the class, and the behaviors are written as methods.
attributes = instance variables: variables that belong to an object of a class behaviors = methods: a function that performs a specific task ex: System.out.print("blah")
Objects are a kind of value that combines data and the code that operates on that data into a single unit.
the properties or what each object knows about itself