Every class has a constructor method, which is used to create objects. The constructor method always has the same exact name as the class. Here are some examples that you’ve seen before:

computer science

Description

CS 0007 (Intro to Programming with Java)

Spring 2020

UTA: Max Dudek (Thursday)

max.dudek@pitt.edu

Office Hours: Wednesday 10-12

 

UTA: Lucas Dalessandro (Friday)

ljd43@pitt.edu

Office Hours: Friday 11-1

 

Lab 10

Classes

So far in this course, we have discussed variable types, which can be put into three groups:

-        Primitive types (int, double, boolean, char)

o   These variable types are the simplest types

o   Notice how they are written using all lowercase letters

-        Arrays (int[], double[], boolean[], String[], etc.)

o   Used to represent a collection of values

o   The value at index i can be fetched using arr[i], where i is an int

o   Arrays also have length, which can be found using arr.length

-        Classes (String, Random, Scanner, etc.)

o   Classes are more complex data types

o   Notice how they all start with a capital letter (this is not technically required by Java, but is done by convention to make it easy to differentiate between classes and primitive types)

o   Variables that are of a class type are called Objects

§  Think of classes as blueprints (a description of a certain type of object), and objects as the specific instances

§  For example:

·       String s = “Max”

·       The variable s is an object

o   They can have certain methods associated with them

§  For example, String objects have methods like .length(), .substring(), .equals(), etc.

§  Scanner objects have methods like .nextLine(), .nextInt(), etc.

§  YOU HAVE USED OBJECTS BEFORE!

Now, we will learn how to write and use custom classes to simplify computational problems.

 

Creating an Object variable

Every class has a constructor method, which is used to create objects. The constructor method always has the same exact name as the class. Here are some examples that you’ve seen before:

-        Scanner keyboard = new Scanner(System.in);

-        Random rand = new Random();

These lines of code have three parts which results in the creation of an object variable:

1.     Declaration of a variable (Scanner keyboard), which specifies the variable type.

2.     The keyword new, which tells Java to create a new object

3.     Text Box: NOTE: If Strings are objects, then why doesn’t String creation look like the examples above? The reason is that Strings are used so often that they have been made a special type of object in Java, which means that we don’t need to write out the full constructor call. So when you write:
String name = “Max”;
It’s actually a shorthand for:
String name = new String(“Max”);
A call to the constructor method (which may or may not have arguments as inputs).

Once an object has been created, you can use any methods associated with the class (such as keyboard.nextLine() or rand.nextDouble() or name.length();

Static vs. non-static methods

You might have noticed that most methods we’ve written have the word “static” in the header, whereas the class methods in this lab do not. These are two types of methods:

-        Non-static methods are called from a specific object. For example:

o   name.length()

o   keyboard.nextLine()

-        Static methods are called directly from a class, and are not specific to any one instance of the class:

o   Math.round();

o   Math.pow();

o   Notice how Math is a class, and you don’t need to create a Math object in order to use these methods – they are called directly from the Math class.

NOTE: If you want to call a method of a class from within that class, you don’t need the variable name and the dot before the method name. This is what we have been doing the entire semester when we wrote methods (for example, the bubbleSort method from Lab9). We don’t need to create an object to call these methods from, because we are calling them from within the Lab9 class where it was written.


Related Questions in computer science category