Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, class properties, also known as fields or attributes, represent the state or data of an object. These properties define the characteristics of an object, and their values can be different for each instance of a class.
In this tutorial, we'll explore how to define and use properties in Java classes.
To define a property, specify the data type followed by the property name inside the class body. You can also use access modifiers (private, protected, and public) to control the visibility of the properties.
Here's an example of a Car
class with three properties:
public class Car { public String make; public String model; private int year; }
In this example, we define three properties for the Car
class: make
, model
, and year
. The make
and model
properties are public, while the year
property is private.
You can access and modify the values of an object's properties using the dot (.) operator. However, the access and modification depend on the property's access modifier.
Here's an example demonstrating how to set and get the values of an object's properties:
public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.make = "Toyota"; myCar.model = "Camry"; // myCar.year = 2022; // Error: The field Car.year is not visible System.out.println("Make: " + myCar.make); // Output: Make: Toyota System.out.println("Model: " + myCar.model); // Output: Model: Camry // System.out.println("Year: " + myCar.year); // Error: The field Car.year is not visible } }
In this example, we can directly access and modify the public properties make
and model
. However, we can't directly access or modify the private property year
.
To access and modify private properties, we can use getter and setter methods. These methods allow us to encapsulate the properties and control how they are accessed and modified.
Here's an example of the Car
class with getter and setter methods for the year
property:
public class Car { public String make; public String model; private int year; // Getter method for the year property public int getYear() { return year; } // Setter method for the year property public void setYear(int year) { this.year = year; } }
Now, we can use the getter and setter methods to access and modify the year
property:
public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.make = "Toyota"; myCar.model = "Camry"; myCar.setYear(2022); System.out.println("Make: " + myCar.make); // Output: Make: Toyota System.out.println("Model: " + myCar.model); // Output: Model: Camry System.out.println("Year: " + myCar.getYear()); // Output: Year: 2022 } }
In summary, class properties in Java represent the state or data of an object. You can define properties with different access modifiers and use getter and setter methods to control how they are accessed and modified. By using class properties, you can create objects with different characteristics and model real-world entities in your code.
Declaring Member Variables in Java Classes: Member variables are fields within a class that store data. Here's how you declare them:
public class Car { // Member variables String make; String model; int year; }
Defining Properties in Java Class Example: Properties are essentially member variables that define the characteristics or attributes of an object.
public class Person { // Properties or member variables String name; int age; }
Scope of Member Variables in Java Classes: The scope of member variables is limited to the class they are declared in. They can be accessed by methods within the same class.
public class Example { // Member variable with class-wide scope private int count; // Method accessing the member variable public void incrementCount() { count++; } }
Access Modifiers for Member Variables in Java:
Access modifiers control the visibility and accessibility of member variables. Common modifiers include public
, private
, protected
, and package-private.
public class Example { public int publicVar; private String privateVar; protected double protectedVar; int defaultVar; // Package-private by default }
Default Values for Member Variables in Java Classes: Member variables are assigned default values if not explicitly initialized.
public class Example { int intValue; // Default: 0 boolean boolValue; // Default: false Object objValue; // Default: null }
Java final
Keyword and Member Variables:
The final
keyword makes a member variable constant and cannot be reassigned.
public class Constants { // Constant member variable public final double PI = 3.14; }
Static Member Variables in Java Classes: Static member variables belong to the class itself rather than instances of the class.
public class Counter { // Static member variable public static int count = 0; // Increment count method public static void incrementCount() { count++; } }