Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Fields
AP® Computer Science A (Java) Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source AP® (Advanced Placement®) Java Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency. It is a substantial amount of coursework that represents a typical year of high school-level study or a semester of a University computer science course. This course may also be used to prepare for the AP® Computer Science A exam.
-->
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
In Java, the "Fields" section of a class is used to house variables and to declare and initialize them to a value, if needed. A Java field is a variable inside a class. For instance, in the class below, we see four different variables:
FieldsExample.java
package exlcode;
public class FieldsExample {
public static int exampleVariableOne = 5;
public static int exampleVariableTwo = 10;
public int exampleVariableThree;
private int exampleVariableFour;
public static void main(String[] args) {
// this prints exampleVariableOne and exampleVariableTwo
System.out.println(exampleVariableOne);
System.out.println(exampleVariableTwo);
}
}
There are multiple ways to declare a variable. We can either declare it or declare and initialize it with an initial value. Here are some examples:
dataType variableName;
We declared a variable and its data type but put nothing in the variable
dataType variableName = initialValue;
We declared a variable, its data type, and put an initial value into memory. The initial value must be of the correct data type.
dataType variableNameOne, variableNameTwo;
We declared two variables, both of the same data type, but put nothing in either variable. We can do this with more than two variables.
We declare two variables, both of the same data type, and put an initial value in each variable. Again, we can do this for more than two variables as long as we follow the same format.
You get to pick the name for each variable in the program. Various things in a program are given names. A name chosen by a programmer is called an "identifier". For example, in the code above, exampleVariableOne is an identifier.