O'Reilly logo

Java Programming for Beginners by Mark Lassoff

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required