Invoking an Object's Method

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

The string is 15 characters long

Invoking an Object's Method

An object consists of both variables (state information) and methods (small programs). Both of these are called members of the object. Java uses dot notation for both:

referenceToAnObject.memberOfObject

For example, to invoke the length() method of the object named str1 do this:

                                                          
len  = str1.length();  

Method names have () at their end. Often there is additional information inside the (), but they are required even if they contain nothing. The above method evaluates to an integer, 15, which is assigned to the int variable len.

QUESTION 10:

(Thought Question:) Is it possible for a program to have several objects all of the same class?