Java: Fields vs. Properties

Instructor: David Gloag

David has over 40 years of industry experience in software development and information technology and a bachelor of computer science

In this lesson, we will take a look at Java and define Java fields and properties. We'll then show why you might use one over the other. At the end, you should have a good understanding of these important concepts.

Technological Bliss

We use the Internet a lot these days. We seem to have an incessant need to stay connected with each other. Take a quick look you around for proof of this. It's likely you'll see people texting, maybe sending and receiving email, or they might be browsing Facebook or some other social media site. But how do these connections happen? How do your emails or texts get to their intended recipient? How does a post get posted? Most people are blissfully unaware of how this happens. Those that know realize there is a collection of computers and software making it all happen. One type of software that facilitates this is Java.

What is Java?

Java is a computer programming language that was modified in 1999 to take advantage of the Internet. Sun Microsystems created it in 1991 for general purpose software development, under the name OAK. It was used in things like tablets, cell phones, and television set-top boxes. When those applications didn't go anywhere, it was modified for new uses. It is considered a high-level language, meaning that each statement has a one-to-many relationship with machine language. Today, Java exists on over three billion devices and is supported by millions of developers.

What is a Java Field?

A Java field is a block of memory that has a name, type, and size that corresponds to it. For example, consider the following:

int tomato;

The preceding declaration tells us that there has been a fixed amount of memory allocated, the name of that memory is 'tomato', and that memory will hold an integer value. Further, the field can be used to modify the information stored. For example:

tomato = 5;
tomato = tomato + 5;

Nothing out of the ordinary here. The field 'tomato' contains the integer value 10. Now, an astute observer will notice that a field is much like a Java variable. This is correct, but they do have some differences. Notably, their scope or visibility is different. Fields scope to the class they are defined in, variable scope to the method they are defined in.

What is a Java Property?

A Java property is also much like a field. The real difference is in their intended scope. Fields are meant to be private or protected in scope, meaning that access is restricted. Properties are meant to be public in scope, meaning that access is not restricted. Consider the following code snippet:

public class Test {
  private string _testField;
  public string testProperty {
   get {
    return _testField;
   }
   set {
    _testField = value;
   }
  }
}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support