GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - Class Attributes
Selected Reading
Java - Class AttributesJava Class AttributesJava class attributes are the variables that are bound in a class i.e., the variables which are used to define a class are class attributes. A class attribute defines the state of the class during program execution. A class attribute is accessible within class methods by default. For example, there is a class "Student" with some data members (variables) like roll_no, age, and name. These data members are considered class attributes. Creating (Declaring) Java Class AttributesTo create (declare) a class attribute, use the access modifier followed by the data type and attribute name. It's similar to declaring a variable. SyntaxUse the below syntax to declare a class attribute: access_modifier type attribute_name; Example: Declaring Java Class Attributespublic class Dog {
String breed;
int age;
String color;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
In above class, we've fields like breed, age, and color which are also known as class attributes. Accessing Java Class AttributesTo access the class attribute, you need to create an object first and then use the dot (.) operator with the object name. Class attributes can be also called within the class methods directly. SyntaxUse the below syntax to access a class attribute: object_name.attribute_name; Example: Accessing Java Class AttributesConsider this example, demonstrating how to access the class attributes. class Dog {
// Declaring and initializing the attributes
String breed = "German Shepherd";
int age = 2;
String color = "Black";
}
public class Main {
public static void main(String[] args) {
// Creating an object of the class Dog
Dog obj = new Dog();
// Accessing class attributes & printing the values
System.out.println(obj.breed);
System.out.println(obj.age);
System.out.println(obj.color);
}
}
OutputGerman Shepherd 2 Black Modifying Java Class AttributesTo modify a class attribute, access the attribute and assign a new value using the assignment (=) operator. SyntaxUse the below syntax to modify a class attribute: object_name.attribute_name = new_value; Example: Modifying Java Class AttributesConsider this example, demonstrating how to modify the class attributes. class Dog {
// Declaring and initializing the attributes
String breed = "German Shepherd";
int age = 2;
String color = "Black";
}
public class Main {
public static void main(String[] args) {
// Creating an object of the class Dog
Dog obj = new Dog();
// Accessing class attributes & printing the values
System.out.println("Before modifying:");
System.out.println(obj.breed);
System.out.println(obj.age);
System.out.println(obj.color);
// Modifying class attributes
obj.breed = "Golden Retriever";
obj.age = 3;
obj.color = "Golden";
// Printing
System.out.println("\nAfter modifying:");
System.out.println(obj.breed);
System.out.println(obj.age);
System.out.println(obj.color);
}
}
OutputBefore modifying: German Shepherd 2 Black After modifying: Golden Retriever 3 Golden Making Java Class Attributes Read OnlyYou can also make the class attributes read-only by using the final keyword after the access modifier while declaring an attribute. SyntaxUse the below syntax to make class attribute read-only: access_modifier final data_type attribute_name; Example: Making Java Class Attributes Read OnlyIn the below example, the name attribute is set to read-only using the final keyword. Now this attribute can not be modified and JVM will complain if we try to modify this attribute. package com.tutorialspoint;
class Dog {
final String name = "Tommy";
}
public class Tester {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Tommy"; // Error while modifying name
System.out.println(dog.name);
}
}
OutputCompile and run Tester. This will produce the following result − Exception in thread "main" java.lang.Error: Unresolved compilation problem: The final field Dog.name cannot be assigned at com.tutorialspoint.Tester.main(Tester.java:10) Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |