Java Fields vs. Java Methods

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

There is a difference between a field and a method in Java, although they are both key components of Java classes. This lesson will cover the features and differences of both, providing working code examples.

Fields and Methods

Java fields and methods are core components of the programming language. Each plays a major role in Java classes. Before we compare the two, let's briefly go over some of the key features of fields and methods.

Java Fields

At its most basic, a Java field is a variable. This means it represents a value, such as text or a numeric value. For example, the variable isbn could be declared to hold the ISBN number of a publication. Fields are declared within classes of your code.

public class Book {
  String isbn;
  String title;
  int pageCount;
  double price;
}

Note that each variable has its own type, which defines what type of data can be stored in the field. This is a requirement. The types include String, int, double, long, boolean, and others.

Java Methods

A method is a function. That is, it is a block of code that carries out an operation. Like fields, methods need to be inside classes. A method can accept values from other parts of the program, and they can send results back to other parts of the program.

// Below is a method within Book
public void printNotice() {
  System.out.println("I'm a Book!");
}

A method can also have a type, as a field would. If the method is not going to be sending any information to other parts of the program, the type is set as void. However, if a method will be accepting and returning information, you need to specify which type of information. Let's see how this works with parameters and returning information.

If a method is set up to receive information, these values are called parameters. A parameter is a field that is sent to and from methods. The following code creates a method that will return an integer value. Therefore, it needs to have a type of int when the method is defined.

public int setCount(int c, String isbn) {
  int count = c + 15;
  String myIsbn = isbn;
  return c;
}

Now that we have an understanding of fields and methods, let's compare these two Java concepts.

Fields vs. Methods

Fields and methods are alike in that they exist within classes and have a defined type. If a method returns no values, its type is set to void. The most important distinction between the two is that Java fields can hold information, while Java methods perform a task. On its own, a field only describes what type of information is stored in it.

Methods and fields can also be declared with other modifiers. The type is always required when declaring a field (or void if a method does not return a value). You may have noticed the word public in the method declaration code. This keyword tells Java that the method can be accessed from anywhere in the program. When declaring fields, you can also add the modifier which restricts (or opens up) access to the field. Take a look at the code below, which uses comments to explain each of the modifier types for fields:

public class FieldExamples {
  /* private means that only code in
  this class can access the field */
  private int locationID;
  /* protected: only child classes (sub-classes)
  of this class can access this field */
  protected double price;
  /* public: all classes can access */
  public String author;
  /* static: variable exists in the class, which
  means any instance of that variable exists
in every instance of the class */
  static String publicationType;
  /* final: the value CANNOT change. This is
  used for constant values, because the value can never change.
  Often used in combo with static. Constants are often all-caps */
  static final String PUB_TYPE = "Book";
}

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