Java Class Attributes
The term "variable" for x in the example (as shown below) is actually
an attribute of the class. Or you could say that class attributes are variables
within a class:
Example
Create a class called "MyClass" with two attributes: x and y:
public class MyClass {
int x = 5;
int y = 3;
}
Another term for class attributes is fields.
Accessing Attributes
You can access attributes by creating an object of the class, and by using the
dot syntax (.):
The following example will create an object of the MyClass class, with the
name myObj. We use the x attribute on the object to print its value:
Example
Create an object called "myObj" and print the value of x:
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
Run example »
Modify Attributes
You can also modify attribute values:
Example
Set the value of x to 40:
public class MyClass {
int x;
public static void main(String[] args) {
MyClass myObj = new MyClass();
myObj.x = 40;
System.out.println(myObj.x);
}
}
Run example »
Or override existing values:
Example
Change the value of x to 25:
public class MyClass {
int x = 10;
public static void main(String[] args) {
MyClass myObj = new MyClass();
myObj.x = 25; // x is now 25
System.out.println(myObj.x);
}
}
Run example »
If you don't want the ability to override existing values, declare the attribute
as final:
Example
public class MyClass {
final int x = 10;
public static void main(String[] args) {
MyClass myObj = new MyClass();
myObj.x = 25; // will generate an error: cannot assign a value to
a final variable
System.out.println(myObj.x);
}
}
Run example »
The final keyword is useful when you want a variable to always store the same
value, like PI (3.14159...).
The final keyword is called a "modifier". You will learn more about these in
the Java Modifiers Chapter.
Multiple Objects
If you create multiple objects of one class, you can change the attribute values
in one object, without affecting the attribute values in the other:
Example
Change the value of x to 25 in myObj2, and leave x in myObj1 unchanged:
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj1 = new MyClass(); // Object 1
MyClass myObj2 = new MyClass(); // Object 2
myObj2.x = 25;
System.out.println(myObj1.x); // Outputs 5
System.out.println(myObj2.x); // Outputs 25
}
}
Run example »
Multiple Attributes
You can specify as many attributes as you want:
Example
public class Person {
String fname = "John";
String lname = "Doe";
int age = 24;
public static void main(String[] args) {
Person myObj = new Person();
System.out.println("Name: " + myObj.fname + " " + myObj.lname);
System.out.println("Age: " + myObj.age);
}
}
Run example »
The next chapter will teach you how to create class methods and how to access
them with objects.
Java Constructors
A constructor in Java is a special method that is used to initialize objects. The
constructor is called when an object of a class is created. It can be used to set
initial values for object attributes:
Example
Create a constructor:
// Create a MyClass class
public class MyClass {
int x; // Create a class attribute
// Create a class constructor for the MyClass class
public MyClass() {
x = 5; // Set the initial value for the class attribute x
}
public static void main(String[] args) {
MyClass myObj = new MyClass(); // Create an object of class MyClass (This
will call the constructor)
System.out.println(myObj.x); // Print the value of x
}
}
// Outputs 5
Run example »
Note that the constructor name must match the class name, and it cannot
have a return type (like void).
Also note that the constructor is called when the object is created.
All classes have constructors by default: if you do not create a class constructor
yourself, Java creates one for you. However, then you are not able to set initial
values for object attributes.
Constructor Parameters
Constructors can also take parameters, which is used to initialize attributes.
The following example adds an int y parameter to the constructor. Inside the
constructor we set x to y (x=y). When we call the constructor, we pass a
parameter to the constructor (5), which will set the value of x to 5:
Example
public class MyClass {
int x;
public MyClass(int y) {
x = y;
}
public static void main(String[] args) {
MyClass myObj = new MyClass(5);
System.out.println(myObj.x);
}
}
// Outputs 5
Run example »
You can have as many parameters as you want:
Example
public class Car {
int modelYear;
String modelName;
public Car(int year, String name) {
modelYear = year;
modelName = name;
}
public static void main(String[] args) {
Car myCar = new Car(1969, "Mustang");
System.out.println(myCar.modelYear + " " + myCar.modelName);
}
}
// Outputs 1969 Mustang