Class Attributes In Java
Java class attributes are discussed in order to meet learners’ needs more effectively.
Java Class Attributes
The term “variable” was used in the previous chapter for mrx (as shown below). This is actually a class attribute. A class attribute can also be thought of as a variable within a class.
Creating a class with two attributes: ample and mrx:
public class Main {
double ample= 2.45;
double mrx = 4.11;
}
A class attribute can also be referred to as a field.
Attribute Access
To access Java class attributes, you must create an object of the class and use the dot syntax (.) .
In the following example, an object with the name my_Object will be created from the Main class. To print the value of an object, we use its mrx attribute.
Make an object called “my_Object” and print mrx:
public class Main {
int mrx =2*6/3;public static void main(String[] args) {
Main my_Object = new Main();
System.out.println("Result: "+my_Object.mrx);
}
// Result: 4
}
Now create an object called “my_Object” and print the product of first five natural numbers:
public class Main {
int ample =1*2*3*4*5;public static void main(String[] args) {
Main my_New_Object = new Main();
System.out.println("Result: "+my_New_Object.ample);
}
// Result: 120
}
Modify Attributes
As far as Java class attributes are concerned, you can also change their values.
Set the value of the String variable ample to Java Language:
public class Main {
String ample;public static void main(String[] args) {
Main my_Object = new Main();
my_Object.ample = "Java Language";
System.out.println("Output: "+my_Object.ample);
}
// Output: Java Language
}
Alternatively, you can override existing values:
public class Main {
String ample="Java Language";public static void main(String[] args) {
Main my_Object = new Main();
my_Object.ample = "Java Programming Language";
System.out.println("Output: "+my_Object.ample);
}
// Output: Java Programming Language
}
Similarly, this technique can be used to modify the value of an integer as shown in the example below.
The value of mrx is updated from 23 to 51 as shown in the example below:
public class Main {
int mrx=23;public static void main(String[] args) {
Main my_Object = new Main();
System.out.println("Old value of mrx is: "+my_Object.mrx);
my_Object.mrx = 51; // The value of mrx is modified here from 23 to 51
System.out.println("Updated value of mrx is: "+my_Object.mrx);
}
// Output 1: Old value of mrx is: 23
// Output 2: Updated value of mrx is: 51
}
We can also modify different data types at the same time as shown below.
Following example shows changing the value of String ample from Oop to Object Oriented Programming and integer mrx from 52 to 77:
public class Main {
String ample="OOP";
int mrx= 52;public static void main(String[] args) {
Main my_Object = new Main();
System.out.println("Previous value of ample: "+my_Object.ample);
my_Object.ample = "Object-Oriented Programming";
System.out.println("Modified value of ample: "+my_Object.ample);
System.out.println("Previous value of mrx: "+my_Object.mrx);
my_Object.mrx = 77;
System.out.println("Modified value of mrx: "+my_Object.mrx);}
// Output:// Previous value of ample: OOP
// Modified value of ample: Object-Oriented Programming
// Previous value of mrx: 52
// Modified value of mrx: 77
}
Defining an attribute as final prevents the attribute from being overridden:
public class Main {
final int mrx = 13;public static void main(String[] args) {
Main my_Object1 = new Main();
my_Object1.mrx = 16; //
System.out.println(my_Object1.mrx);
}
// Output : cannot assign a value to final variable mrx
}
public class Main {
final String ample_name = "Java";public static void main(String[] args) {
Main my_Object1 = new Main();
my_Object1.ample_name = "Java Programming";
System.out.println(my_Object1.ample_name);
}
}
If a variable should always store the same value, like PI (3.14159…), you should use the final keyword.
“Modifier” is the name of the final keyword. The Java Modifiers Chapter will teach you more about them.
Multiple Objects
It is possible to change the attribute values of one object without affecting those of another if you create multiple objects of one class.
Make my_Second_Object’s mrx “Modifiers” while leaving my_First_Object’s mrx unchanged:
public class Main {
String mrx = "Final";public static void main(String[] args) {
Main my_First_Object = new Main(); // Object 1
Main my_Second_Object = new Main(); // Object 2
my_Second_Object.mrx = "Modifiers";
System.out.println(my_First_Object.mrx); // Output1: Final
System.out.println(my_Second_Object.mrx); // Output2: Modifiers
}
}
Make my_Second_Object’s ample “17.77” while leaving my_First_Object’s ample unchanged:
public class Main {
float ample = 14.4f;public static void main(String[] args) {
Main my_First_Object = new Main(); // Object 1
Main my_Second_Object = new Main(); // Object 2
my_Second_Object.ample = 17.77f;
System.out.println(my_First_Object.ample); // Output1: 14.4
System.out.println(my_Second_Object.ample); // Output2: 17.77
}
}
Multiple Attributes
In Java class attributes, There is no limit to the number of attributes you can specify:
public class Main {
float mrx = 14.34f;
float ample = 15.24f;
String mrx_name= "Java Attributes";public static void main(String[] args) {
Main my_Object = new Main();
System.out.println("Topic Name: " + my_Object.mrx_name);
System.out.println("Sum of Float numbers: " + (my_Object.mrx+ my_Object.ample));
}
// Output1: Topic Name: Java Attributes
// Output2: Sum of Float numbers: 29.58
}
public class Main {
double mrx_double = 14.3232332131234d;
int ample_int=45;int mrx_int=13;
double ample_double = 15.24f;
String mrx_name= "Multiple Value attributes";public static void main(String[] args) {
Main my_Object = new Main();
System.out.println("Topic Name: "+my_Object.mrx_name);
System.out.println("Product of int Numbers: "+(my_Object.ample_int * my_Object.mrx_int));
System.out.println("Sum of Double value: "+(my_Object.ample_double + my_Object.mrx_double));}// Output1: Topic Name: Multiple Value attributes
// Output2: Product of int Numbers: 585
// Output3: Sum of Double value: 29.563232984241566
}