programs
programs
programs
Java 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.
Syntax
void barking() {
}
void hungry() {
}
1|Page
void sleeping() {
}
}
In above class, we've fields like breed, age, and color which are also
known as class attributes.
Syntax
object_name.attribute_name;
class Dog {
// Declaring and initializing the attributes
String breed = "German Shepherd";
int age = 2;
String color = "Black";
}
2|Page
System.out.println(obj.age);
System.out.println(obj.color);
}
}
Output
German Shepherd
2
Black
Syntax
object_name.attribute_name = new_value;
class Dog {
// Declaring and initializing the attributes
String breed = "German Shepherd";
int age = 2;
String color = "Black";
}
3|Page
// 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);
// Printing
System.out.println("\nAfter modifying:");
System.out.println(obj.breed);
System.out.println(obj.age);
System.out.println(obj.color);
}
}
Output
Before modifying:
German Shepherd
2
Black
After modifying:
Golden Retriever
3
Golden
AD
4|Page
Making Java Class Attributes Read Only
You can also make the class attributes read-only by using
the final keyword after the access modifier while declaring an
attribute.
Syntax
class Dog {
final String name = "Tommy";
}
Output
Compile and run Tester. This will produce the following result −
Syntax
Example
6|Page
Here is the source code of the above defined method
called minimum(). This method takes two parameters n1 and n2 and
returns the minimum between the two −
class Util {
/** the snippet returns the minimum between two numbers */
public int minimum(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
Syntax
object_name.method_name([parameters]);
Example
class Util {
public int minimum(int n1, int n2) {
7|Page
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
public class Tester {
Output
Minimum value = 6
8|Page
Differentiate the instance variables from local variables if they
have same names, within a constructor or a method.
class Student {
int age;
Student(int age) {
this.age = age;
}
}
Call one type of constructor (parametrized constructor or
default) from other in a class. It is known as explicit constructor
invocation.
class Student {
int age
Student() {
this(20);
}
Student(int age) {
this.age = age;
}
}
Tester() {
System.out.println("This is an example program on keyword this");
}
Tester(int num) {
9|Page
// Invoking the default constructor
this();
Example
11 | P a g e
}
Output
The variable's scope refers to the region where they are created and
accessed in a given program or function. The variable scope also
refers to its lifetime.
12 | P a g e
public int getAge( ) {
// access the instance variable
return puppyAge;
}
Compile and run the program. This will produce the following result
−
Output
Puppy Age :2
13 | P a g e
public void setAge( int age ) {
// access the instance variable and modify it
puppyAge = age;
}
Compile and run the program. This will produce the following result
−
Output
Puppy Age :2
Breed :Bulldog
14 | P a g e
Example: Scope of Java Class (Static) Variables
// local variables
int a = 10;
int b = 20;
int c = a + b;
15 | P a g e
}
Compile and run the program. This will produce the following result
−
Output
Puppy Age :2
Breed :Bulldog
c: 30
16 | P a g e