Creating member variables

Let's start with the first category. Let's tell Java what information we'd like to store in Person:

package gettingobjectoriented; 
 
public class Person { 
    public String firstName; 
    public String lastName; 
} 

Telling Java what information to store is a lot like declaring variables in any other piece of code. Here, we've given the Person class two member variables; these are pieces of information that we can access in any Person object.

Just about everything we declare in a class declaration needs to be given a protection level. When we become more advanced Java users, we'll begin to use different protection levels, but for now, we're simply going to declare everything "public."

So, as we've set it up here, every Person ...

Get Java Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.